越来越checkboxId动态创建的复选框? [英] getting checkboxId for dynamically created checkbox?
问题描述
我已经动态创建checkbox.I想选择checkbox.but我只得到最后的复选框Id的标识。
对于如。如果5复选框,然后创建它显示5复选框的标识,只有当clicked.I希望所有5复选框中的ID。结果
下面是我的动态创建CheckBox类。
块引用>私人无效getcheckbox()
{
INT cnter;
linearMain =(的LinearLayout)findViewById(R.id.linearLayout2);
字母=新的LinkedHashMap<字符串,字符串>(); 为(cnter = 0; cnter&下; str_arr) 如果(!optionsarray.get(cnter).isEmpty())
{
cnter ++;
}其他
打破; 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);
设置<>设定= alphabet.entrySet();
迭代器<> I = set.iterator();
INT CNT = 0;
而(CNT< cnter)
{
@燮pressWarnings(rawtypes)
最后Map.Entry的我=(Map.Entry的)i.next();
复选框=新的复选框(本);
checkBox.setId(的Integer.parseInt(me.getKey()的toString()));
checkBox.setTextColor(getResources()的getColor(R.color.black));
checkBox.setText(me.getValue()的toString());
linearMain.addView(复选框);
CNT ++; }
下面是我的另一个类检查selescted复选框标识
块引用>checkBox.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v){ INT checkBoxId =((复选框)V).getId(); 开关(checkBoxId){
情况1: selectedcheckboxid =A;
testAnswernew(selectedcheckboxid);
打破; 案例2: selectedcheckboxid =B;
testAnswernew(selectedcheckboxid);
打破;
案例3:
selectedcheckboxid =C;
testAnswernew(selectedcheckboxid);
打破;
情况4:
selectedcheckboxid =D;
testAnswernew(selectedcheckboxid);
打破;
情况5: selectedcheckboxid =E;
testAnswernew(selectedcheckboxid);
打破;
情况6:
selectedcheckboxid =F;
testAnswernew(selectedcheckboxid);
打破;
案例7:
selectedcheckboxid =G;
testAnswernew(selectedcheckboxid);
打破;
案例8:
selectedcheckboxid =H;
testAnswernew(selectedcheckboxid);
打破;
案例9:
selectedcheckboxid =我;
testAnswernew(selectedcheckboxid);
打破;
案例10:
selectedcheckboxid =J;
testAnswernew(selectedcheckboxid);
打破;
默认: }
解决方案此问题可以当我们应用的setOnClickListener来解决的母公司上创建动态线性布局的孩子复选框。我一直在使用添加复选框的getLayoutInflater()的,并为每个检查设置ID box.For它ü可以看到下面code。
的for(int i = 0; I< JAS.length();我++){ 查看孩子= getLayoutInflater()膨胀(R.layout.return_child,NULL);
CHK =(复选框)child.findViewById(R.id.Product);
chk.setText(c.optString(名字));
chk.setId(ⅰ); ll.addView(小孩); chk.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
字符串CHK = NULL;
CHK = Integer.toString(v.getId());
如果(((复选框)ⅴ).isChecked()){
selchkboxlist.add(CHK); }其他{
selchkboxlist.remove(CHK);
}
}
});
}下面selchkboxlist是的列表的
下同列表selchkboxlist =新的ArrayList();
现在在点击链接就可以申请低于code访问选中的复选框的ID。
ReturnProducts.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){ 为(中间体p值= 0; P&下; selchkboxlist.size(),P ++){
串长度= selchkboxlist.get(对);
INT LN =的Integer.parseInt(长度);
Toast.makeText(ReturnOrders.this,+ LN,Toast.LENGTH_LONG).show();
} }
});I have dynamically created checkbox.I want Id of selected checkbox.but I am getting Id of only last checkbox. for eg. if 5 checkboxes created then it is showing Id of 5th checkbox only when clicked.I want Id of all 5 checkbox.
Here is my dynamic created checkbox class.
private void getcheckbox() { int cnter; linearMain = (LinearLayout) findViewById(R.id.linearLayout2); alphabet = new LinkedHashMap<String, String>(); for (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<?> i = 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 is my another class for 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: }
解决方案This problem can be solved when we apply "setOnClickListener" on child check box of parent dynamic created Linear layout. I have added check box using "getLayoutInflater()" and set id for each check box.For it u can see below code.
for (int i = 0; i < JAS.length(); i++) { View child = getLayoutInflater().inflate(R.layout.return_child, null); chk = (CheckBox) child.findViewById(R.id.Product); chk.setText(c.optString("name")); chk.setId(i); ll.addView(child); chk.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String chk = null; chk = Integer.toString(v.getId()); if (((CheckBox) v).isChecked()) { selchkboxlist.add(chk); } else { selchkboxlist.remove(chk); } } }); }
Here selchkboxlist is "List" same as below :
List selchkboxlist = new ArrayList();
Now On button click you can apply below code for accessing id of selected check box.
ReturnProducts.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int p=0;p<selchkboxlist.size();p++){ String Length = selchkboxlist.get(p); int ln = Integer.parseInt(Length); Toast.makeText(ReturnOrders.this, ""+ln, Toast.LENGTH_LONG).show(); } } });
这篇关于越来越checkboxId动态创建的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!