android - Selecting a specific checkbox in a long listview checks multiple checkboxes -


i have listview checkbox. click on particular item, corresponding checkbox gets selected, selects checkboxes repeatedly after every 8-10 rows in listview. understand happens because in android, views screen visible stored memory optimization. have not been able overcome situation. pls suggest.

public class fbfriendslistadaptor extends arrayadapter<fbfrienditem>{      private final context context;      private static list<fbfrienditem> items;      private sparsearray<fbfrienditem> checkeditems = new sparsearray<fbfrienditem>();      viewholder holder;       public fbfriendslistadaptor(context context, list<fbfrienditem> items) {          super(context, r.layout.fb_friend_list, items);          this.items = items;          this.context = context;      }       @override      public fbfrienditem getitem(int position) {         // todo auto-generated method stub         return items.get(position);      }       public view getview(final int position, view convertview, viewgroup parent) {           view v = convertview;           if (v == null) {              layoutinflater vi = (layoutinflater)context.getsystemservice(context.layout_inflater_service);              v = vi.inflate(r.layout.fb_friend_list, parent, false);          }           holder = new viewholder();          holder.selectfriendcb = (checkbox)v.findviewbyid(r.id.selectfriendcb);          v.settag(holder);           v.setonclicklistener(new view.onclicklistener(){             public void onclick(view v) {                 if (checkeditems.get(position) != null){                     checkeditems.remove(position);                     holder.selectfriendcb.setchecked(false);                 }                 else {                     checkeditems.put(position, items.get(position));                     holder.selectfriendcb.setchecked(true);                 }             }          });           fbfrienditem item = items.get(position);          textview firstnametv = (textview) v.findviewbyid(r.id.firstnametv);          textview lastnametv = (textview) v.findviewbyid(r.id.lastnametv);          imageview piciv = (imageview) v.findviewbyid(r.id.piciv);          if (!item.getfbid().isempty()) {              firstnametv.settext(" " + item.getfirstname());              lastnametv.settext(" " + item.getlastname());              bitmap image = item.getimage();              if(image != null) {                  piciv.setimagebitmap(image);              }          }           return v;      }       public sparsearray<fbfrienditem> getcheckeditems(){          return checkeditems;      }       static class viewholder {          checkbox selectfriendcb;              } } 

the first thing i'm noticing is

     if (!item.getfbid().isempty()) {          firstnametv.settext(" " + item.getfirstname());          lastnametv.settext(" " + item.getlastname());          bitmap image = item.getimage();          if(image != null) {              piciv.setimagebitmap(image);          }      } 

setting these values in cases big no-no. remember views being reused, if ever set of these properties, can't count on them ever having default value. might still set last item. try logic looks more this:

     if (!item.getfbid().isempty()) {          firstnametv.settext(" " + item.getfirstname());          lastnametv.settext(" " + item.getlastname());          bitmap image = item.getimage();          if(image == null) {              // reset default because view might have last displayed              // item non-default value              piciv.setimagebitmap(mydefaultimage);          } else {              piciv.setimagebitmap(image);          }      } else {          // reset defaults because view might have last displayed          // item non-default value          firstnametv.settext(null);          lastnametv.settext(null);          piciv.setimagebitmap(mydefaultimage);      } 

the next thing i'm noticing management of checked items wrong. whether item checked or not state should managed listview, not adapter. don't know you're trying achieve, i'm not going specify how lay out items, want start removing checkbox elements , listview.setchoicemode(). http://developer.android.com/reference/android/widget/abslistview.html#setchoicemode%28int%29


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -