android - Getting multiple value of checked check boxes in an array -
android - Getting multiple value of checked check boxes in an array -
i have created check boxes programmatically.
here code:
llmain = (linearlayout) findviewbyid(r.id.linearlayoutmain); llayout = new linearlayout[b]; (int j = 0; j < b; j++) { int x = 0; x = x + (j * 5); llayout[j] = new linearlayout(checkboxdemo.this); llayout[j].setlayoutparams(new layoutparams( layoutparams.wrap_content, layoutparams.wrap_content)); llayout[j].setorientation(linearlayout.vertical); llmain.addview(llayout[j]); (int = x; < x + 5; i++) { if (x > a) { break; } else { if (testarraylist.contains(idsplit[i])) { cb = new checkbox(checkboxdemo.this); cb.settext(namesplit[i]); cb.setid(i + 1); cb.setchecked(true); cb.settextcolor(color.black); cb.settextsize(12f); cb.setbuttondrawable(r.drawable.checkbox); cb.setpadding(35, 5, 25, 5); cb.settag(i + 1); cb.setoncheckedchangelistener(handlecheck(cb)); if ((count1.equals(1)) || (count1.equals(2))) { cb.setenabled(true); } else { cb.setenabled(false); } llayout[j].addview(cb); } else { cb = new checkbox(checkboxdemo.this); cb.settext(namesplit[i]); cb.setid(i + 1); cb.settextcolor(color.black); cb.settextsize(12f); cb.setbuttondrawable(r.drawable.checkbox); cb.setpadding(35, 5, 25, 5); cb.settag(i + 1); cb.setoncheckedchangelistener(handlecheck(cb)); if ((count1.equals(1)) || (count1.equals(2))) { cb.setenabled(true); } else { cb.setenabled(false); } llayout[j].addview(cb); } } } } now have received value of 15 check boxes database. now, check boxes non- selected. if click on 5 of them; want value of these 5- selected check boxes in array, on click of button.
how can implement this???????
first of instead of initiating same checkbox object 1 time again , again, create array of checkbox
checkbox[] cbs; also create array of int variables (you can utilize vector/arraylist here)
arraylist<integer> selectedcheckboxes = new arraylist<integer>(); // here alter integer per info type of tag (by default int) before loop, initialize array number of checkboxes want add together in linearlayout
cbs = new checkbox[<<put size here>>]; now in loop, initialize each checkbox
cbs[<<put index variable here>>] = new checkbox(checkboxdemo.this); e.g. cbs[i] = new checkbox(checkboxdemo.this); //here iteration variable cbs[i].settext(namesplit[i]); cbs[i].settag(i + 1); cbs[i].setid(i + 1); cbs[i].setchecked(true); cbs[i].settextcolor(color.black); cbs[i].settextsize(12f);
in click listener event of button run loop size of checkbox array , check if each of checkbox selected or not. if checkbox selected add together tag value in arraylist have created
for(int = 0; < cbs.lenght(); i++){ if(cbs[i].ischecked()){ selectedcheckboxes.add(cbs[i].gettag()); } } you values of selected checkboxes in arraylist.
android android-checkbox
Comments
Post a Comment