I'm creating a multiple choice AlertDialog
, using AlertDialog.Builder
and setMultiChoiceItems
.
I want to check / uncheck items from inside the OnMultiChoiceClickListener
, but I can't find how.
Here's my code:
final List<User> users = Util.getUsers();
final String[] names = new String[users.size()];
final boolean[] checked = new boolean[users.size()];
for (int i=0; i < names.length; i++) {
names[i] = users.get(i).getName();
checked[i] = selectedUsers.contains(users.get(i).getId());
}
new AlertDialog.Builder(EventFormActivity.this)
.setTitle(R.string.schedule_for)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
updateScheduleForText();
}
})
.setMultiChoiceItems(names, checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) selectedUsers.add(users.get(which).getId());
else selectedUsers.remove((Integer) users.get(which).getId());
// check or uncheck other items? how?
}
})
.show();
The only way I see that I could do this is implement a custom ListAdapter
, or even a custom dialog-themed Activity
.