1

当我单击一个按钮时,会弹出一个带有多选复选框的对话框。当我选择并单击确定时,它会弹出另一个包含所选项目的对话框。如果我通过单击对话框外部关闭对话框并再次按下按钮以显示对话框以进行选择并选择并单击确定,它将附加先前选择的结果。我不想附加结果。我不理解或犯了什么错误?代码在这里

@Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_tab2, container, false);
        Button button = (Button) view.findViewById(R.id.displayBox);
        final List<String> selectedItems = new ArrayList<String>();
        final String[] itemList = {"Item1", "Item2", "Item3", "Item1", "Item2", "Item3", "Item1", "Item2", "Item3", "Item1", "Item2", "Item3"};
        final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
        builder.setTitle("Multi Select");
        builder.setMultiChoiceItems(itemList, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    selectedItems.add(itemList[which].toString());
                }
            }
        }).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ListView listView = new ListView(view.getContext());
                listView.setAdapter(new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, selectedItems));
                Dialog dialog1 = new Dialog(view.getContext());
                dialog1.setTitle("Selected Informations");
                dialog1.setContentView(listView);
                dialog1.show();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.show();
            }
        });
        return view;
    }
4

1 回答 1

-1

尝试这个,

ListView listView = new ListView(view.getContext());
                listView.setAdapter(new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, selectedItems));

//清除你的列表视图

selectedItems.clear();

            Dialog dialog1 = new Dialog(view.getContext());
            dialog1.setTitle("Selected Informations");
            dialog1.setContentView(listView);
            dialog1.show();
于 2015-11-16T06:34:28.987 回答