mercredi 13 janvier 2016

How to check if CheckBoxes are empty in an Array?

I have a couple of CheckBoxes:

    checkBox1 = (CheckBox)findViewById(R.id.one);
    checkBox2 = (CheckBox)findViewById(R.id.two;
    checkBox3 = (CheckBox)findViewById(R.id.three);
    checkBox4 = (CheckBox)findViewById(R.id.four);
    checkBox5 = (CheckBox)findViewById(R.id.five);
    checkBox6 = (CheckBox)findViewById(R.id.six);
    checkBox7 = (CheckBox)findViewById(R.id.seven);

Then I add them into an array:

    List<CheckBox> checkBoxes = new ArrayList<>();
    CheckBox checkBox;        
    checkBoxes.add(checkBox1); checkBoxes.add(checkBox2);
    checkBoxes.add(checkBox3); checkBoxes.add(checkBox4);
    checkBoxes.add(checkBox5); checkBoxes.add(checkBox6);
    checkBoxes.add(checkBox7);

I want to make a Toast if one of the CheckBoxes are empty on a button click. So I use a for loop inside a button:

public void onButtonClick(){
        for(int i = 0; i <checkBoxes.size(); i++){
            checkBox = checkBoxes.get(i);
        }
       if(!checkBox.isChecked){
            //make Toast "Hey, you didn't check a box"
      }else{
           //do something based on the checked box.
      }
  }

The problem is the Toast is still being shown if a CheckBox is checked and it is not executing the else code. Any help would be appreciated, thanks.




Aucun commentaire:

Enregistrer un commentaire