Amardeepvijay Amardeepvijay - 3 months ago 26
Android Question

Getting multiple value of checked check boxes in an array

I have created check boxes programmatically.

Here is my code:

llmain = (LinearLayout) findViewById(R.id.linearLayoutMain);
lLayout = new LinearLayout[b];
for (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]);
for (int i = x; i < 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 I have received value of 15 check boxes from the database.



Now, all the check boxes are non- selected. If I click on any 5 of them; then I want to get the value of these 5- selected check boxes all together in an array, on just a click of a button.

How can I implement this???????

Answer

First of all instead of initiating same CheckBox object again and again, create Array of Checkbox

  CheckBox[] cbs;

Also create array of int variables (you can use Vector/ArrayList here)

  ArrayList<Integer> selectedCheckboxes = new ArrayList<Integer>(); // here change Integer as per data type of Tag (by default its int)

Before for loop, initialize this array with number of checkboxes you want to add in LinearLayout

  cbs = new CheckBox[<<put size here>>];

Now in for loop, initialize each checkbox

  cbs[<<put index variable here>>] = new CheckBox(CheckBoxdemo.this);

e.g. cbs[i] = new CheckBox(CheckBoxdemo.this); //here i is 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 your button run loop with size of CheckBox array and check if each of checkbox is selected or not. If checkbox is selected then add its TAG value in ArrayList we have created

  for(int i = 0; i < cbs.lenght(); i++){
      if(cbs[i].isChecked()){
          selectedCheckboxes.add(cbs[i].getTag());
      }
  }

You will get values of all selected checkboxes in arraylist.