Checkbox unchecked when I scroll listview on Android -
i new android development. created listview textbox , checkbox.
when check checkbox , scroll down check other items in list view, older ones unchecked.
how avoid problem in listview? please guide me code.
here code:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:id="@+id/textview01" android:layout_height="wrap_content" android:text="list of items" android:textstyle="normal|bold" android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent"/> <listview android:id="@+id/listview01" android:layout_height="250px" android:layout_width="fill_parent"/> <button android:text="save" android:id="@+id/btnsave" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> this xml page used create dynamic list row:
listview.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:gravity="left|center" android:layout_width="wrap_content" android:paddingbottom="5px" android:paddingtop="5px" android:paddingleft="5px"> <textview android:id="@+id/textview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textcolor="#ffff00" android:text="hi"/> <textview android:text="hello" android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10px" android:textcolor="#0099cc"/> <edittext android:id="@+id/txtbox" android:layout_width="120px" android:layout_height="wrap_content" android:textsize="12sp" android:layout_x="211px" android:layout_y="13px"/> <checkbox android:id="@+id/chkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> this activity class.
customlistviewactivity.java:
public class customlistviewactivity extends activity { listview lstview; static context mcontext; button btnsave; private static class efficientadapter extends baseadapter { private layoutinflater minflater; public efficientadapter(context context) { minflater = layoutinflater.from(context); } public int getcount() { return country.length; } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.listview, parent, false); holder = new viewholder(); holder.text = (textview) convertview .findviewbyid(r.id.textview01); holder.text2 = (textview) convertview .findviewbyid(r.id.textview02); holder.txt = (edittext) convertview.findviewbyid(r.id.txtbox); holder.cbox = (checkbox) convertview.findviewbyid(r.id.chkbox1); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text.settext(curr[position]); holder.text2.settext(country[position]); holder.txt.settext(""); holder.cbox.setchecked(false); return convertview; } public class viewholder { textview text; textview text2; edittext txt; checkbox cbox; } } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); lstview = (listview) findviewbyid(r.id.listview01); lstview.setadapter(new efficientadapter(this)); btnsave = (button)findviewbyid(r.id.btnsave); mcontext = this; btnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // want print text in listview 1 one. // later insert in database. // // toast.maketext(getbasecontext(), "edittext value, checkbox value , other values", toast.length_short).show(); (int = 0; < lstview.getcount(); i++) { view listorderview; listorderview = lstview.getchildat(i); try{ edittext txtamt = (edittext)listorderview.findviewbyid(r.id.txtbox); checkbox cbvalue = (checkbox)listorderview.findviewbyid(r.id.chkbox1); if(cbvalue.ischecked()== true){ string amt = txtamt.gettext().tostring(); toast.maketext(getbasecontext(), "amount :"+amt, toast.length_short).show(); } } catch (exception e) { // todo: handle exception } } } }); } private static final string[] country = { "item1", "item2", "item3", "item4", "item5", "item6","item7", "item8", "item9", "item10", "item11", "item12" }; private static final string[] curr = { "1", "2", "3", "4", "5", "6","7", "8", "9", "10", "11", "12" }; } please me solve problem.
i have referred problem in many places. not proper answer solve problem.
please provide me code avoid unchecking checkbox while scrolling , down.
in custom listview adapter call setchecked after calling setoncheckedchangelistener.
this way sever link old listener recycled view.
Comments
Post a Comment