android - CheckBox in ListView multiple select -
good day all.
i have list view checkboxes near each textview. when check 1 checkbox , scroll down, random other checkboxes checked. i've read post , couple others here , state of each checkbox needs saved in boolean list @ getview()
. how go doing that?
thanks.
edited code:
this code of adapter, containing getview()
method.
public class mycustomadapter extends baseadapter { public arraylist<error> list; private context mcontext; private layoutinflater minflator; public mycustomadapter(context context, int textviewresourceid, arraylist<error> list) { this.list = list; this.mcontext = context; minflator = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); } @override public int getcount() { return list.size(); } @override public error getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if(convertview == null) { holder = new viewholder(); convertview = minflator.inflate(r.layout.listviewrow, null); holder.text = (textview)convertview.findviewbyid(r.id.text); holder.checkbox = (checkbox)convertview.findviewbyid(r.id.checkbox); convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } final int pos = position; holder.text.settext(list.get(position).getdescription()); holder.checkbox.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { list.get(pos).setchecked(ischecked); } }); return convertview; } static class viewholder { textview text; checkbox checkbox; } }
below code working me
my mainactivity
public class mainactivity extends activity { private listview llchb; private string[] data = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; private arraylist<string> arrdata=null; private arraylist<inforowdata> infodata; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); arrdata=new arraylist<string>(); arrdata.add("a"); arrdata.add("b"); arrdata.add("c"); arrdata.add("d"); arrdata.add("e"); arrdata.add("f"); arrdata.add("g"); arrdata.add("h"); arrdata.add("i"); arrdata.add("j"); arrdata.add("k"); arrdata.add("l"); arrdata.add("m"); arrdata.add("n"); arrdata.add("o"); arrdata.add("p"); llchb = (listview) findviewbyid(r.id.llchb); infodata = new arraylist<inforowdata>(); (int = 0; < data.length; i++) { infodata.add(new inforowdata(false, i)); // system.out.println(i); //system.out.println("data == "+data[i]); } llchb.invalidate(); llchb.setadapter(new myadapter()); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } public class myadapter extends baseadapter { @override public int getcount() { // todo auto-generated method stub return data.length; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @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 row = null; row = view.inflate(getapplicationcontext(), r.layout.row, null); textview tvcontent=(textview) row.findviewbyid(r.id.tvcontent); //tvcontent.settext(data[position]); tvcontent.settext(data[position]); //system.out.println("the text here like.. == "+tvcontent.gettext().tostring()); final checkbox cb = (checkbox) row .findviewbyid(r.id.chbcontent); cb.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub if (infodata.get(position).isclicked) { infodata.get(position).isclicked = false; } else { infodata.get(position).isclicked = true; } for(int i=0;i<infodata.size();i++) { if (infodata.get(i).isclicked) { system.out.println("selectes == "+ data[i]); } } } }); if (infodata.get(position).isclicked) { cb.setchecked(true); } else { cb.setchecked(false); } return row; } } }
my inforowdata model class
public class inforowdata { public boolean isclicked=false; public int index; /*public string fanid; public string stramount;*/ public inforowdata(boolean isclicked,int index/*,string fanid,string stramount*/) { this.index=index; this.isclicked=isclicked; /*this.fanid=fanid; this.stramount=stramount;*/ } }
my mainxml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <listview android:id="@+id/llchb" android:layout_width="match_parent" android:layout_height="wrap_content" > </listview> </relativelayout>
this custom rowxml listview
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlmain" android:layout_width="fill_parent" android:layout_height="fill_parent" > <relativelayout android:id="@+id/rlinner" android:layout_width="fill_parent" android:layout_height="50dp" > <textview android:id="@+id/tvcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="20dp" android:text="medium text" android:textcolor="#000000" /> <checkbox android:id="@+id/chbcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="10dp" /> </relativelayout> </relativelayout>
Comments
Post a Comment