getTag() 的适配器问题,返回 null
这是我
public class MyAdapter extends ResourceCursorAdapter implements OnScrollListener {
适配器中的标头我像这样设置了标签,
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = super.newView(context, cursor, parent);
final MyCache cache = new MyCache();
view.setTag(cache);
}
而不是我有一些方法
public void metA(){
//here I want to read the tag
//how can I do that ?
}
我还实现了滚动侦听器
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//how to read for example the third item in the list ?
//item.getTag() returns null
}
我尝试使用 getItem(0) 但我也收到空指针异常... 在 onScroll 方法中读取标签的正确方法是什么?视图实际包含什么? 我知道我做了一些非常愚蠢的事情,但我无法弄清楚。
this is my header
public class MyAdapter extends ResourceCursorAdapter implements OnScrollListener {
in my adapter I set the tag like this
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = super.newView(context, cursor, parent);
final MyCache cache = new MyCache();
view.setTag(cache);
}
than I have some method
public void metA(){
//here I want to read the tag
//how can I do that ?
}
I also impplement scroll listenter
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//how to read for example the third item in the list ?
//item.getTag() returns null
}
I tried with getItem(0) but I also receive null pointer exceptions...
What is the right way to read the tags in the onScroll method, what the view actually contains ?
I know I do something very stupid but I can't figured out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于
getTag()
方法,您应该尝试从View
声明中删除final
关键字,这可能是问题所在(我不是100%确定)。另外,我不确定MyCache()
类正在做什么,以及为什么需要在每个View
上设置一个MyCache
对象。也许仅添加一个MyCache
实例作为适配器类变量会是更好的解决方案。对于
getItem()
方法,请确保您已正确实现此方法,并且从对象列表中返回一个项目。要从
View
获取标签,您只需使用:Edit: 在
getView()< 中获取
View 标签
/code> 方法,只需使用getView()
方法的参数convertView
即可:For the
getTag()
method you should try to remove thefinal
keyword from youView
declaration, this may be the problem (I'm not 100% sure). Also I'm not sure whay yoruMyCache()
class is doing and why you need to set aMyCache
object on eachView
. Maybe it would be a better solution to just add oneMyCache
instance, as an adapter class variable.And for the
getItem()
method be sure that you have implemented properly this method, and that you return an item from your list of objects.To get the tag from a
View
you just need to use :Edit: to get the
View tag
in yourgetView()
method, just use the parameterconvertView
of thegetView()
method: