asynchronous - OnItemClick listener selects multiple rows in async filled list android -
i have list filled contacts. load contacts in list async class.
in list class have setonitemclicklistener implemented. in setonitemclicklistener place selected contact in array can sent text them. working dream.
but there small problem. when selecting contact in list there multiple contacts selected this:
-normal contact- -selected contact- -normal contact- -normal contact- -normal contact- -normal contact- -selected contact-
i load contacts code:
@override protected void onpostexecute(arraylist<list_item_person> contacts) { // todo auto-generated method stub super.onpostexecute(contacts); pd.cancel(); personlistadapter adapter = new personlistadapter(getactivity().getapplicationcontext(), r.layout.list_item_person, contacts); list.setadapter(adapter); }
this async class
and onclick this:
list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { list_item_person o = (list_item_person) list.getitematposition(position); searchresults selected = new searchresults(""); int = 0; for( searchresults number:contact_selected){ if(o.phonenumber.equals(number.getphonenumber())){ ++; selected = number; } }; if(i == 0) { contact_selected.add(new searchresults(string.valueof(o.phonenumber))); arg1.setbackgroundcolor(color.red); log.e("e", string.valueof(o.phonenumber)); }else{ contact_selected.remove(selected); arg1.setbackgroundcolor(color.parsecolor("#162229")); }
what forgetting here?
you victim nr #### of itmes/views recycled when user scrolls list. can read lot 'feature' on site , many blogs.
your code should remember (for instance in boolean array or arraylist) 'selected' state of every item. add boolean selected = false list_item_person
too. in onclick() change value.
then in getview() set color accordingg selected state too.
to running first concentrate on keeping selected state. remove or comment out copying arrays , so.