如何将Checkbox值设置为数组android

时间:2014-07-29 13:46:12

标签: java android arrays checkbox arraylist

我得到了ArrayList

ArrayList<CheckBox> swmsInfo = new ArrayList<CheckBox>();

和一些复选框:

checkBoxCompany = createCheckBox(R.id.checkBoxCompany);

checkBoxName = createCheckBox(R.id.checkBoxName);
checkBoxPhone = createCheckBox(R.id.checkBoxPhone);


checkBoxAdress = createCheckBox(R.id.checkBoxAdress);

如果检查了某些复选框,我想得到它们的值0或1,当我得到它们的值时,我想将它们放入数组中。我的复选框位于数组

ArrayList<CheckBox> boxes = new ArrayList<CheckBox>();

1 个答案:

答案 0 :(得分:0)

您可以遍历arraylist并获取值。像

这样的东西
for (int i = 0; i<boxes.size(); i++){
    CheckBox cb = boxes.get(i);
    if (cb.isChecked()){
      System.out.println("checked");
      //from here you can populate you array
    }else{
      System.out.println("not checked");
      //from here you can populate you array
    }
}

=======评论后编辑=======

public class CheckBoxStatus{

    CheckBox cb;
    boolean blnIsChecked;

    public CheckBoxStatus(CheckBox cb, boolean blnIsChecked){
      this.cb = cb;
      this.blnIsChecked = blnIsChecked;
    }

    public CheckBox getCheckbox(){
       return this.cb;
    }

    public boolean getStatus(){
       return this.blnIsChecked;
    }

    public boolean setStatus(boolean blnIsChecked){
        this.blnIsChecked = blnIsChecked;
    }

}

//Now in your code instead of ArrayList<CheckBox>, use ArrayList<CheckBoxStatus>

ArrayList<CheckBoxStatus> alCBS = new ArrayList<CheckBoxStatus>();
CheckBoxStatus cbs = new CheckBoxStatus(<checkbox instance>, false); //pass false as default value;
alCBS.add(cbs);

//Modified loop
for (int i = 0; i<boxes.size(); i++){
  CheckBox cb = boxes.get(i);
  CheckBoxStatus cbs = new CheckBoxStatus(cb, cb.isChecked());
  boxes.set(i, cbs);
}