开发者

Adapter problem with getTag() ,return null

开发者 https://www.devze.com 2023-04-06 08:20 出处:网络
this is my header public class MyAdapter extends ResourceCursorAdapter implements OnScrollListener { in my adapter I set the tag like this

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 metho开发者_运维技巧d, what the view actually contains ? I know I do something very stupid but I can't figured out.


For the getTag() method you should try to remove the final keyword from you View declaration, this may be the problem (I'm not 100% sure). Also I'm not sure whay yoru MyCache() class is doing and why you need to set a MyCache object on each View. Maybe it would be a better solution to just add one MyCache 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 :

   (MyCache)view.getTag();

Edit: to get the View tag in your getView() method, just use the parameter convertView of the getView() method:

       public View getView(int position,View convertView,ViewGroup parent){
            if(convertView!=null)
                 (MyCache)convertView.getTag();
            // code....
       }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号