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
.
The DialogInterface that you get in the OnClick Method should be an AlertDialog. You can then use the getButton function to get the button objects and you should be able to manipulate the various buttons from there.
EDIT: I think I understand the issue now, you are going to want to work with the ListView While putting together an example I found this already solved on SO That example show you how to check/uncheck the list items from your OnMultiChoiceClickListener
Try this : How uncheck items in AlertDialog (setMultiChoiceItems)?.
AskMeSelected[which] = false;
alertDialogList.setItemChecked(which, false);
Be sure to also set the corresponding position of boolean array to false.
精彩评论