[英]Show list selected item on positive button pressed when AlertDialog re opens
I have an alert dialog in which contains multiple list selection listener. 我有一个警报对话框,其中包含多个列表选择侦听器。 Problem is that when i open first dialog after selection of multiple items i press positive button then on re open alert dialog it show nothing selected what i selected items when last opened.
问题是,当我选择了多个项目后打开第一个对话框时,我按下肯定按钮,然后在重新打开警报对话框中没有显示上次打开时我选择了哪些项目。
final CharSequence[] dialogList= list.toArray(new CharSequence[list.size()]);
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(SchoolFieldsData.this);
builderDialog.setTitle("Enter Average Fee");
int count = dialogList.length;
boolean[] is_checked = new boolean[count]; // set is_checked boolean false;
// Creating multiple selection by using setMutliChoiceItem method
builderDialog.setMultiChoiceItems(dialogList, is_checked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked) {
}
});
builderDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListView list = ((AlertDialog) dialog).getListView();
// make selected item in the comma seprated string
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < list.getCount(); i++) {
boolean checked = list.isItemChecked(i);
if (checked) {
if (stringBuilder.length() > 0) stringBuilder.append(",");
stringBuilder.append(list.getItemAtPosition(i));
}
}
/*Check string builder is empty or not. If string builder is not empty.
It will display on the screen.
*/
if (stringBuilder.toString().trim().equals("")) {
// ((TextView) findViewById(R.id.text)).setText("Click here to open Dialog");
stringBuilder.setLength(0);
} else {
// ((TextView) findViewById(R.id.text)).setText(stringBuilder);
}
}
});
builderDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ((TextView) findViewById(R.id.text)).setText("Click here to open Dialog");
}
});
AlertDialog alert = builderDialog.create();
alert.show();
This is because every time this code ins invoked a new AlertDialog is built (line 2) and new array of is_checked
is provided to it. 这是因为每次调用此代码ins时,都会构建一个新的AlertDialog(第2行),并
is_checked
提供新的is_checked
数组。
Try to restore the state, for example keep is_checked
array in different place and inside the positive button handler save the state to this array (instead of the line boolean checked = list.isItemChecked(i);
). 尝试恢复状态,例如将
is_checked
数组保留在不同的位置,并在肯定按钮处理程序内将状态保存到此数组(而不是boolean checked = list.isItemChecked(i);
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.