android - Change CheckBox state on List item click? -
i have list , checkbox in every row of it. want whenever click on row, checkbox changes state accordingly.
checkbox.xml
<checkbox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:text="" />
click listener list
protected void onlistitemclick(listview l, view v, int position, long id) { toast.maketext(getapplicationcontext(), "you have selected item no." + (position + 1) + "", toast.length_short).show(); super.onlistitemclick(l, v, position, id); selectedrow = position; if (v == null) { layoutinflater lif = (layoutinflater) getsystemservice(context.layout_inflater_service); v = lif.inflate(r.layout.posting_detail_question_row, null); } if (checkedtextview.ischecked()) { toast.maketext(getapplicationcontext(), "already voted", toast.length_short).show(); } else { string[] = {"option", "votes"}; // values fetch // hashmap int[] = {r.id.option, r.id.votes}; // id's view simpleadapter adp = new simpleadapter(getapplicationcontext(), hashlist, r.layout.posting_detail_question_row, from, to); setlistadapter(adp); checkedtextview.setchecked(true); } }
posting_detail_question_row.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="fill_parent" android:background="@drawable/poll_border_top_bg" android:orientation="horizontal"> <textview android:id="@+id/option" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_marginleft="10dp" android:layout_weight=".3" android:gravity="center_vertical" android:text="answer 1" android:textcolor="#333333" android:textsize="15sp" /> <textview android:id="@+id/votes" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_marginleft="10dp" android:layout_weight="1" android:gravity="center_vertical" android:textcolor="#333333" android:textsize="15sp" /> <checkbox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:text="" /> </linearlayout> </linearlayout>
my adapter simpleadapter:
simpleadapter adp = new simpleadapter(getapplicationcontext(), hashlist, r.layout.posting_detail_question_row, from, to); log.v("mylog", "after simple adapter"); setlistadapter(adp);
you need use findviewbyid pointer checkbox:
protected void onlistitemclick(listview l, view v, int position, long id) { toast.maketext(getapplicationcontext(), "you have selected item no." +(position+1)+"", toast.length_short).show(); super.onlistitemclick(l, v, position, id); if (v != null) { checkbox checkbox = (checkbox)v.findviewbyid(r.id.checkbox); checkbox.setchecked(!checkbox.ischecked()); } }
if recieving onclick event v
should never null have put on appropriate checks.
Comments
Post a Comment