java - How can I associate a value with each checkbox in a ListView? -
i have created custom listview using custom adapter. have xml file defines each row, , each row has checkbox defined in xml file. app judging app each item on listview "task" counts number of points. idea if task completed, judge clicks checkbox, , score of task added overall score.
unfortunately, see no way value associated checkbox. there way this? i'll post code, , hope it's enough general idea of issue.
the xml file row:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/score_list_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <checkbox android:id="@+id/score_box" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignparentright="true" android:paddingtop="30dp" android:scalex="2" android:scaley="2" android:onclick="chkboxclicked" /> <textview android:id="@+id/subtask" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toleftof="@id/score_box" android:paddingright="30dp" android:textsize="20sp"/> <textview android:id="@+id/max_points" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/subtask" /> </relativelayout>
the method activity creates list:
... public void createscoringlist() { listview scorelist = (listview) findviewbyid(r.id.score_list); listview scorelistpartial = (listview) findviewbyid(r.id.score_list_partial); arraylist<scoringinfo> objlist = new arraylist<scoringinfo>(); arraylist<scoringinfo> objlistpartial = new arraylist<scoringinfo>(); scoringinfo scrinfo; (int = 0; < subtasklist.size(); i++) { subtask_num = subtasklist.get(i).subtask_num; max_points = subtasklist.get(i).max_points; partial_points_allowed = subtasklist.get(i).partial_points_allowed; task_name = subtasklist.get(i).task_name; scrinfo = new scoringinfo(); scrinfo.setmaxpoints("max points: " + max_points); scrinfo.setsubtask(task_name); if (partial_points_allowed == 1) objlistpartial.add(scrinfo); else objlist.add(scrinfo); } scorelist.setadapter(new scorelistadapter(objlist , this)); scorelistpartial.setadapter(new scorelistadapter2(objlistpartial, this)); }
if more code required clarity, ask, , provide. didn't want overflow question loads of code think may unnecessary.
you can either store value in model (i think it's called scoringinfo) or can assign value every checkbox using settag("score", value)
method , read calling gettag("score")
.
you can set , read tag in adapter class this. adapter shall implement onclicklistener
, manage list of scoringinfo
items.
public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = layoutinflater.from(this) .inflate(r.layout.<your_layout>, parent, false); } scoringinfo item = this.getitem(position); checkbox checkbox = (checkbox) convertview.findviewbyid(r.id.checkbox_id); checkbox.settag("score", item.max_points); checkbox.setonclicklistener(this); } public void onclick(view view) { if (view instanceof checkbox) { boolean checked = ((checkbox) view).ischecked(); int score = view.gettag("score"); // rest } }
Comments
Post a Comment