java - how to store value of checkboxes when creating a custom listView android -
public class applesupportlist extends baseadapter { private layoutinflater minflater; private final string[] values; public applesupportlist(context context, string[] values) { minflater = layoutinflater.from(context); this.values = values; } @override public int getcount() { // todo auto-generated method stub return values.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub final viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.customlist1, null); holder = new viewholder(); holder.text1 = (textview) convertview.findviewbyid(r.id.labelcontact1); holder.img = (imageview) convertview.findviewbyid(r.id.imagecontact1); holder.check = (checkbox) convertview.findviewbyid(r.id.checkboxcontact1); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text1.settext(values[position]); holder.check.setid(position); convertview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (holder.check.ischecked()) { holder.check.setchecked(false); //store id } else { holder.check.setchecked(true); //remove id } } }); holder.check.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub checkbox cb = (checkbox) v; if (cb.ischecked()) { //store id } else { //remove id } } }); return convertview; } static class viewholder { textview text1; imageview img; checkbox check; } }
i creating custom list view check boxes.all works fine except when scroll list check boxes checked/unchecked automatically. how can store state of check boxes. have gone through related questions nothing worked me. please take @ code , me out.
your custom adapter must implement compoundbutton.oncheckedchangelistener
. use sparsebooleanarray
.
drawing romain guy's solution @
https://groups.google.com/forum/?fromgroups#!topic/android-developers/no0lrgj6q2m
then
cb.setchecked(mcheckstates.get(position, false)); cb.setoncheckedchangelistener(this);
then use checked state set text check box
public boolean ischecked(int position) { return mcheckstates.get(position, false); } public void setchecked(int position, boolean ischecked) { mcheckstates.put(position, ischecked); } public void toggle(int position) { setchecked(position, !ischecked(position)); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { mcheckstates.put((integer) buttonview.gettag(), ischecked); }
use below example reference , modify according requirements. have not used viewholder in sample. use view holder used in code.
example
public class mainactivity extends activity implements adapterview.onitemclicklistener { int count; private checkboxadapter mcheckboxadapter; string[] genres = new string[] { "action", "adventure", "animation", "children", "comedy", "documentary", "drama", "foreign", "history", "independent", "romance", "sci-fi", "television", "thriller" }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final listview listview = (listview) findviewbyid(r.id.lv); listview.setitemscanfocus(false); listview.settextfilterenabled(true); listview.setonitemclicklistener(this); mcheckboxadapter = new checkboxadapter(this, genres); listview.setadapter(mcheckboxadapter); button b= (button) findviewbyid(r.id.button1); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub stringbuilder result = new stringbuilder(); for(int i=0;i<genres.length;i++) { if(mcheckboxadapter.mcheckstates.get(i)==true) { result.append(genres[i]); result.append("\n"); } } toast.maketext(mainactivity.this, result, 1000).show(); } }); } public void onitemclick(adapterview parent, view view, int position, long id) { mcheckboxadapter.toggle(position); } class checkboxadapter extends arrayadapter implements compoundbutton.oncheckedchangelistener { private sparsebooleanarray mcheckstates; layoutinflater minflater; textview tv1,tv; checkbox cb; string[] gen; checkboxadapter(mainactivity context, string[] genres) { super(context,0,genres); mcheckstates = new sparsebooleanarray(genres.length); minflater = (layoutinflater)mainactivity.this.getsystemservice(context.layout_inflater_service); gen= genres; } @override public int getcount() { // todo auto-generated method stub return gen.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub view vi=convertview; if(convertview==null) vi = minflater.inflate(r.layout.checkbox, null); tv= (textview) vi.findviewbyid(r.id.textview1); cb = (checkbox) vi.findviewbyid(r.id.checkbox1); tv.settext("name :"+ gen [position]); cb.settag(position); cb.setchecked(mcheckstates.get(position, false)); cb.setoncheckedchangelistener(this); return vi; } public boolean ischecked(int position) { return mcheckstates.get(position, false); } public void setchecked(int position, boolean ischecked) { mcheckstates.put(position, ischecked); } public void toggle(int position) { setchecked(position, !ischecked(position)); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { mcheckstates.put((integer) buttonview.gettag(), ischecked); } } }
checkbox.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="15dp" android:layout_margintop="34dp" android:text="textview" /> <checkbox android:id="@+id/checkbox1" android:focusable="false" android:focusableintouchmode="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@+id/textview1" android:layout_marginright="22dp" android:layout_margintop="23dp" /> </relativelayout>
Comments
Post a Comment