checkbox - dynamically adding check box and getting its checked unchecked value in android -
i creating android app there 20 checkboxes shown user multiple selection. have programmed display checkbox dynamically problem how checked box value ? have tried code far.
my global variables are
arraylist<mycheckbox> listofcheckeditem; private int checkbox_position;
and adding checkboxes did this
listofcheckeditem = new arraylist<>(); listofcheckeditem.add(new mycheckbox(true, "one")); //(ischecked,text) listofcheckeditem.add(new mycheckbox(false, "two")); listofcheckeditem.add(new mycheckbox(true, "three")); listofcheckeditem.add(new mycheckbox(false, "four")); listofcheckeditem.add(new mycheckbox(false, "five")); viewgroup.layoutparams params = new viewgroup.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); (int = 0; < listofcheckeditem.size(); i++) { checkbox_position = i; mycheckbox mycheckbox = listofcheckeditem.get(i); checkbox checkbox = new checkbox(this); checkbox.setchecked(mycheckbox.getischecked()); checkbox.settext(mycheckbox.gettext()); selectcategorylayout.addview(checkbox, params); checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { listofcheckeditem.get(checkbox_position).setischecked(ischecked); } }); }
and check boxes value did this
stringbuilder builder = new stringbuilder(); (int = 0; < listofcheckeditem.size(); i++) { mycheckbox mcb = listofcheckeditem.get(i); builder.append("\nstatus: " + mcb.gettext() + " " + mcb.getischecked()); } resultview.settext(builder.tostring());
for first time when run app every checkboxes shown correctly , when click on button check boxes (checked/unchecked) value shows correct result, when check/uncheck 1 of checkboxes , again click on button result last checkbox value changed. missing cannot figure out.
problem solved, instead of doing this
checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { listofcheckeditem.get(checkbox_position).setischecked(ischecked);// changed line below code } });
i did this
checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { mycheckbox.setischecked(ischecked);//i changed line , every thing worked fine } });
Comments
Post a Comment