Maveňツ
Maveňツ

Reputation: 1

How to get value of selected checked boxes which are created programatically in android

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

Upvotes: 0

Views: 850

Answers (3)

Gal Rom
Gal Rom

Reputation: 6471

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 ...


Just need to remove list_sted and use Utstyr while setting up the tags rest all was ok.

Upvotes: 1

Rashad
Rashad

Reputation: 11197

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.. :)

Upvotes: 2

Finder
Finder

Reputation: 1217

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(); 
      }

}

Upvotes: 0

Related Questions