我有一个alertDialog,提示用户进行多项选择,我可以做几个选择并保存在arrayList中的所选项目但是当我再次触发该alertBox时,它会被重置并且所有的复选框都是unChecked,我希望它们能够保留我离开它们的状态,直到我手动重置它们。这是如何运作的? 我的代码:
ArrayList<String> participants = db.getNames();
final String[] arr = participants.toArray(new String[participants
.size()]);
boolean[] checkedItems = new boolean[participants.size()];
final ArrayList<String> selectedParticipants = new ArrayList<String>();
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Select the participants:");
b.setMultiChoiceItems(arr, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
selectedParticipants.add(arr[which].toString());
}
}
});
b.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(EnterExpense.this,
"" + selectedParticipants, Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
您的上述代码是正确的,但只有一件事是您更新boolean [] checkedItems
数组列表中的已检查项目。一旦用户选择了所有参与者,就可以获得所选参与者的位置,并将所有这些位置添加到chekedItems
列表中,其余项目一旦完成就会将其设为假,这肯定会有效。
您可以使用getCheckedItemIds()
或getCheckedItemPositions ()
从列表视图中获取已检查项目的列表。您应该先使用AlertDialog.getListView()
来获取对话框的列表视图。
b.getListView().getCheckedItemPositions ();