从多选项对话框界面中删除项目不起作用



我正在尝试使用一个对话框来显示使用multiChoiceItems的ArrayList。我想检查要删除的项目,单击按钮删除/关闭对话框,并在下次打开对话框时删除这些项目。

当我调试下面的代码时,我发现我为每个选定的项目都经历了循环,但实际上没有删除任何项目。我觉得我在这里错过了一些可能非常微不足道的东西,我希望得到一些帮助!

我知道如果我删除多个项目,我必须考虑到索引的变化,所以不用担心。我可以自己删除第一个项目后,我会这样做!

多谢!

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Title");
            mSelectedItems.clear();
            builder.setMultiChoiceItems(myList,null,
                    new DialogInterface.OnMultiChoiceClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog,int which, boolean isChecked){
                            if (isChecked) {
                                // If the user checked the item, add it to the selected items
                                mSelectedItems.add(which);
                            } else if (mSelectedItems.contains(which)) {
                                // Else, if the item is already in the array, remove it
                                mSelectedItems.remove(Integer.valueOf(which));
                            }
                        }
                    });
            builder.setPositiveButton("Remove from list", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    for(int i=0;i<mSelectedItems.size();i++){
                        arrayChoices.remove(mSelectedItems.get(i));
                    }
                    myList = arrayChoices.toArray(new CharSequence[arrayChoices.size()]);
                }
            });
            builder.setNegativeButton("End", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();

castted mSelectedItems.get(i) 到 int 并且它起作用了。它显示mSelectedItems.get(i)是调试中的一个整数,尽管这让我想知道为什么它还没有工作。

最新更新