Code which i hv used to create the checkboxes

try{
for (int i = 0; i < Utstyr.size(); i++) {
cb = new CheckBox(getApplicationContext());
cb.setText(""+Utstyr.get(i));
cb.setTextColor(Color.parseColor("#000000"));
cb.setTag(""+list_sted.get(i));
cb.setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle);
checkbox_lay.addView(cb);
}}
catch(Exception e){
 System.out.println("ohh i got busted...!!!");
}

How to get the value of which checkbox is being selected.. i want h name of the checkbox

有帮助吗?

解决方案

CheckBox[] chkArray = new CheckBox[Utstyr.size()];//
for (int i = 0; i < Utstyr.size(); i++) {
    chkArray[i] = new CheckBox(getApplicationContext());
    chkArray[i].setText(""+Utstyr.get(i));
    chkArray[i].setTextColor(Color.parseColor("#000000"));
    chkArray[i].setTag(""+Utstyr.get(i));
    chkArray[i].setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle);
    checkbox_lay.addView(chkArray[i]);
}
for (int k = 0; k < Utstyr.size(); k++){
    if(chkArray[k].isChecked()){
        //Do something
    }
}

Hope this helps.. :)

其他提示

To generate checkbox with in loop,you don't need to set globalize variable.

for (int i = 0; i < Utstyr.size(); i++) {
cb = new CheckBox(getApplicationContext());
......
}

instead of above code you have to initialize like below.

 for (int i = 0; i < Utstyr.size(); i++) {
    CheckBox cb = new CheckBox(getApplicationContext());
    ......
    }

to Get selected check box you have to use following code

int childcount = checkbox_lay.getChildCount();
for (int i=0; i < childcount; i++){
      View v = checkbox_lay.getChildAt(i);

     if(v instanceof Checkbox){

    Checkbox ck=(Checkbox)v;
       boolean isSelected = ch.isChecked(); 
      }

}

Well you need reference to those checkboxes. Creat an array of checkboxes and add it everytime you creat one, and than you can get what you need from them ...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top