Why is my Android ListView selecting multiple items? -


i'm making simple listview in android shows dialog. it's purpose user select item saved sharedpreference. next time user opens same list, want item selected last time highlighted.

my problem when create list using arrayadapter, single out index selected last time, apply custom drawable background show item saved, listview shows 3 items "selected". it's first item, last item, , item want selected.

here image showing what's going on

image01image02

here code arrayadapter. using viewholder pattern.

public class adaptercolors extends arrayadapter<coloritem> {      private context context;      private coloritem[] objects;      private int layoutresource;       private static class viewholder {          imageview icon;          textview color;      }       public adaptercolors(context context, int resource, coloritem[] objects) {          super(context, resource, objects);          this.context = context;          this.layoutresource = resource;          this.objects = objects;      }       @override      public view getview(int position, view convertview, viewgroup parent) {          coloritem coloritem = getitem(position);          viewholder viewholder;           if (convertview == null) {              viewholder = new viewholder();              layoutinflater inflater = layoutinflater.from(getcontext());              convertview = inflater.inflate(layoutresource, parent, false);              viewholder.icon = (imageview) convertview.findviewbyid(r.id.imageview_icon);              viewholder.color = (textview) convertview.findviewbyid(r.id.textview_item_name);              convertview.settag(viewholder);          } else {              viewholder = (viewholder) convertview.gettag();          }           viewholder.icon.setimageresource(coloritem.getcoloriconid());          viewholder.color.settext(coloritem.getcolorname());           if(position == 2) {              convertview.setbackgroundresource(r.drawable.list_item_selected);          }           return convertview;      }  } 

r.drawable.list_item_selected custom drawable.

as test, i'm picking out position of 2 can see in getview() method. item @ position 2 gets backgroundresource set, first , last items set! going on here!?

if needs see other code, i'll happy update question.

thanks.

you need give unselected image except position want selected in case position 2. call notifydatasetchange of baseadapter refresh listview after clicking on listitem or selected item.

@override      public view getview(int position, view convertview, viewgroup parent) {          coloritem coloritem = getitem(position);          viewholder viewholder;           if (convertview == null) {              viewholder = new viewholder();              layoutinflater inflater = layoutinflater.from(getcontext());              convertview = inflater.inflate(layoutresource, parent, false);              viewholder.icon = (imageview) convertview.findviewbyid(r.id.imageview_icon);              viewholder.color = (textview) convertview.findviewbyid(r.id.textview_item_name);              convertview.settag(viewholder);          } else {              viewholder = (viewholder) convertview.gettag();          }           viewholder.icon.setimageresource(coloritem.getcoloriconid());          viewholder.color.settext(coloritem.getcolorname()); // need give image unselected state too.inplace of below line can give below line in else part of positioin== 2 condition convertview.setbackgroundresource(r.drawable.list_item_unselectedimage);          if(position == 2) {              convertview.setbackgroundresource(r.drawable.list_item_selected);          }            return convertview;      } 

Comments