Remove array value on unclick
I have check-boxes that save the index of that checkbox on click in an array. I am trying to add functionality so that when the checkbox is unchecked the value is removed from the array.
Below is the loop that saves the checkbox value to the array I am then comparing the selected array with another tag array later in my script. So if the user clicks the checkbox and then unclicks it the value is still true.
CheckBox[] setOfCheckBoxes = new CheckBox[]
{
(CheckBox) findViewById(R.id.checkBox1),
(CheckBox) findViewById(R.id.checkBox2),
(CheckBox) findViewById(R.id.checkBox3),
(CheckBox) findViewById(R.id.checkBox4),
};
for (int i = 0; i < setOfCheckBoxes.length; i++) {
final int index = i;
setOfCheckBoxes[i].setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
selected[index] = ((CheckBox) v).isChecked();
}
});
}
Should I do the unChecked in the开发者_开发问答 same loop?
You're already doing it.
selected[index] = ((CheckBox) v).isChecked();
If your checkbox is checked, your selected[index]
will be true
else it will be false
.
I'm assuming you have initialized selected[]
as new boolean[setOfCheckBoxes.length]
and set them all to false.
Correct me if I'm wrong. You can also get the status of checkboxes:
for (int i = 0; i < setOfCheckBoxes.length; i++) {
selected[i] = setOfCheckBoxes[i].isChecked();
}
精彩评论