android - getting checkboxId for dynamically created checkbox? -
i have dynamically created checkbox.i want id of selected checkbox.but getting id of last checkbox. eg. if 5 checkboxes created showing id of 5th checkbox when clicked.i want id of 5 checkbox.
here dynamic created checkbox class.
private void getcheckbox() { int cnter; linearmain = (linearlayout) findviewbyid(r.id.linearlayout2); alphabet = new linkedhashmap<string, string>(); (cnter = 0; cnter < str_arr;) if (!optionsarray.get(cnter).isempty()) { cnter++; } else break; alphabet.put("1", option_a_new); alphabet.put("2", option_b_new); alphabet.put("3", option_c_new); alphabet.put("4", option_d_new); alphabet.put("5", option_e_new); alphabet.put("6", option_f_new); alphabet.put("7", option_g_new); alphabet.put("8", option_h_new); alphabet.put("9", option_i_new); alphabet.put("10", option_j_new); set<?> set = alphabet.entryset(); iterator<?> = set.iterator(); int cnt = 0; while (cnt < cnter) { @suppresswarnings("rawtypes") final map.entry me = (map.entry) i.next(); checkbox = new checkbox(this); checkbox.setid(integer.parseint(me.getkey().tostring())); checkbox.settextcolor(getresources().getcolor(r.color.black)); checkbox.settext(me.getvalue().tostring()); linearmain.addview(checkbox); cnt++; }
here class checking selescted checkbox id
checkbox.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { int checkboxid = ((checkbox) v).getid(); switch(checkboxid) { case 1: selectedcheckboxid ="a"; testanswernew(selectedcheckboxid); break; case 2: selectedcheckboxid = "b"; testanswernew(selectedcheckboxid); break; case 3: selectedcheckboxid = "c"; testanswernew(selectedcheckboxid); break; case 4: selectedcheckboxid = "d"; testanswernew(selectedcheckboxid); break; case 5: selectedcheckboxid = "e"; testanswernew(selectedcheckboxid); break; case 6: selectedcheckboxid = "f"; testanswernew(selectedcheckboxid); break; case 7: selectedcheckboxid = "g"; testanswernew(selectedcheckboxid); break; case 8: selectedcheckboxid = "h"; testanswernew(selectedcheckboxid); break; case 9: selectedcheckboxid = "i"; testanswernew(selectedcheckboxid); break; case 10: selectedcheckboxid = "j"; testanswernew(selectedcheckboxid); break; default: }
the problem is, adding clicklistener last added checkbox! add listener checkbox in while loop.
Comments
Post a Comment