Reputation: 4055
I am getting the following two errors located in my code below (CheckBox) v.isChecked();
:
Type mismatch: cannot convert from CheckBox to boolean
The method isChecked() is undefined for the type View
for(final int i = 0; i < setOfCheckBoxes.length; i++){
setOfCheckBoxes[i].setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
selected[i] = (CheckBox) v.isChecked();
}
});
}
Not sure what I am doing wrong and if anyone has any ideas please let me know.
Thanks!
Upvotes: 0
Views: 326
Reputation: 28932
Method calls bind tighter in order of operations than casts. Try this:
selected[i] = ((CheckBox) v).isChecked();
Edit: Regarding the use of 'i' in the inner class, use a separate variable for the captured index and the loop iteration. Try this:
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();
}
});
}
Upvotes: 4
Reputation: 1579
adamp is correct. You must do:
selected[i] = ((CheckBox) v).isChecked();
However, you are getting a problem with your variable "i" because it is final. Move "int i = 0" to outside whatever function this code is in (make it a global variable). This will allow you to use it inside the inner class as well.
Upvotes: 1