Android: Listview don't save a state of checkbox -
i check topic similar problem, stil stay in deadpoint. have listview contact list, when scroll down , scroll listview don't save state of choices. checkboxes setting on false. when debug see, when scrolled listview, onclicklistener thinking checkbox signed , reset this. everything, other array position boolean, etc etc , still don't know :( gratefull help:)
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); readcontacts(); contact_database dbhandler = new contact_database( getapplicationcontext(), null, null, 1); numbers.addall( dbhandler.getallnumbers()); system.out.println("oncreate"); listview lv = getlistview(); registerforcontextmenu(lv); lv.settextfilterenabled(true); lv.clearchoices(); myadapter adapter = (new myadapter(this, r.layout.contact_list_sms, contacts)); adapter.notifydatasetchanged(); lv.setadapter(adapter); } /** * when destroy intent send request database, faster! */ @override protected void ondestroy() { super.ondestroy(); (int = 0; < numbers.size(); ++i) system.out.println("@@@@" + numbers.get(i)); swap_contacts(); } class myadapter extends arrayadapter<contact> { layoutinflater inflat; public myadapter(context context, int textviewresourceid, arraylist<contact> objects) { super(context, textviewresourceid, objects); inflat = layoutinflater.from(context); system.out.println("lolllll"); notifydatasetchanged(); } @override public int getitemviewtype(int position) { return (position == this.getcount() - 1) ? 1 : 0; } @override public int getviewtypecount() { return 2; } // if contact exist in database? numbers, in oncreate method // init saved position databse private boolean exist(string x) { (int = 0; < numbers.size(); i++) if (numbers.get(i).compareto(x) == 0) return true; return false; } @suppresslint("newapi") @override public view getview(int position, view convertview, viewgroup parent) { system.out.println("rows !!! ->" + numbers.size()); viewholder holder = null; if (convertview == null) { holder = new viewholder(); convertview = inflat.inflate(r.layout.contact_row, null); holder.textview1 = (textview) convertview .findviewbyid(r.id.name); holder.textview2 = (textview) convertview .findviewbyid(r.id.number); holder.on_off = (checkbox) convertview .findviewbyid(r.id.enable); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } contact = contacts.get(position); log.v("xxx", holder.tostring()); if (it != null) { holder.textview1.settext(contacts.get(position).name); holder.textview2.settext(contacts.get(position).phone); // update state of checbox database if (exist(contacts.get(position).phone)) holder.on_off.setchecked(true); else holder.on_off.setchecked(false); } final int element_position = position; holder.on_off .setoncheckedchangelistener(new oncheckedchangelistener() { // update list when click on @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { string nr_telefonu = contacts.get(element_position).phone; if (ischecked) { numbers.add(nr_telefonu); } else { numbers.remove(nr_telefonu); } } }); return convertview; } @override public contact getitem(int position) { return contacts.get(position); } private class viewholder { textview textview1, textview2; checkbox on_off; public string tostring() { return "-"; } } }
list not save state while scrolling , because each time new object created row item. need explicitly save state of checkbox of pojo class(setter , getter).
in getview() method need check value each checkbox.
@override public view getview(int position, view convertview, viewgroup parent) { ////////////////////
and on setoncheckedchangelistener need save state of checkbox
holder.on_off .setoncheckedchangelistener(new oncheckedchangelistener() { // update list when click on @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // need save state
for more details please refer below blog
http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html
you can change according requirement.
Comments
Post a Comment