繁体   English   中英

当AlertDialog重新打开时,按肯定按钮显示列表中的选定项目

[英]Show list selected item on positive button pressed when AlertDialog re opens

我有一个警报对话框,其中包含多个列表选择侦听器。 问题是,当我选择了多个项目后打开第一个对话框时,我按下肯定按钮,然后在重新打开警报对话框中没有显示上次打开时我选择了哪些项目。

   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();

这是因为每次调用此代码ins时,都会构建一个新的AlertDialog(第2行),并is_checked提供新的is_checked数组。

尝试恢复状态,例如将is_checked数组保留在不同的位置,并在肯定按钮处理程序内将状态保存到此数组(而不是boolean checked = list.isItemChecked(i); )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM