반응형
출처: https://stackoverflow.com/questions/32136029/how-can-i-select-only-one-checkbox-in-a-dynamic-view
Firstly, declare an ArrayList
in the class:
ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();
Then in addGreetingToListView
add every new checkbox to mCheckBoxes
and modify the click listener of the checkbox:
checkBox.setTag(greetings);
mCheckBoxes.add(checkBox);
checkBox.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
for (int i = 0; i < mCheckBoxes.size(); i++) {
if (mCheckBoxes.get(i) == view)
selected_position = i;
else
mCheckBoxes.get(i).setChecked(false);
}
}
else
{
selected_position=-1;
}
}
});
반응형