【问题标题】:Android dialog setMultiChoiceItems /arraylist remove not workingAndroid 对话框 setMultiChoiceItems /arraylist 删除不起作用
【发布时间】:2017-05-28 06:55:09
【问题描述】:

这是我的代码:

public class MainActivity extends AppCompatActivity  {

    ArrayList<String> nameItems, selectedItems;
    ArrayList<Integer> numCheckedArray;
    TextView mTextView;
    Button mButton;
    String[] namesStringArray;
    boolean[] checkedItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameItems = new ArrayList();

        selectedItems = new ArrayList<>();
        numCheckedArray = new ArrayList<>();


        nameItems.add("Test 1");
        nameItems.add("Test 2");
        nameItems.add("Test 3");
        nameItems.add("Test 4");

        namesStringArray = new String[nameItems.size()];

        for (int i =0; i< nameItems.size(); i++){
            namesStringArray[i] = nameItems.get(i);
        }

        checkedItems = new boolean[nameItems.size()];



        mButton =(Button)findViewById(R.id.testButton);
        mTextView =(TextView)findViewById(R.id.testTextView);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Select Your Items");
                builder.setMultiChoiceItems(namesStringArray, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {


                        if (isChecked){
                            //add the position that the user clicked on
                            numCheckedArray.add(which);


                        }else if (numCheckedArray.contains(which)) {
                            //remove the position that the user clicked on
                            numCheckedArray.remove(Integer.valueOf(which));

                        }
                    }
                });

                builder.setCancelable(false);
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {



                        //add selected items into selected item array      still need to figure out how to remove it from the array
                        for (int i = 0; i < numCheckedArray.size(); i++){

                            //if item is already in array and is still checked don't do anything
                            if (selectedItems.contains(nameItems.get(numCheckedArray.get(i))) && checkedItems[numCheckedArray.get(i)] == true){
                                Log.d("test", "do nothing is called");

                            }

                            //if item is already in array and is not checked remove from array
                            else if (selectedItems.contains(nameItems.get(numCheckedArray.get(i))) && checkedItems[numCheckedArray.get(i)] == false){
                                Log.d("test", "Remove is called");
                                selectedItems.remove(nameItems.get(numCheckedArray.get(i)));

                            }

                            //add item to array
                            else {
                                Log.d("test", "add is called");
                                selectedItems.add(nameItems.get(numCheckedArray.get(i)));
                            }
                        }



                        //used to see the values of the arrays

                        for (int i = 0; i < checkedItems.length; i++){
                            Log.d("test", String.valueOf(checkedItems[i]));
                        }
                        for (int i = 0; i < selectedItems.size(); i++){
                            Log.d("test", selectedItems.get(i));
                        }
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
}

我想通过我的程序实现 3 个目标。它们是:

  • 当一个项目被选中并且不在 selectedItems 数组列表中时,当我单击确定时将其添加到数组列表中(这部分工作正常)。

  • 当一个项目被选中并且已经在 selectedItems 数组列表中时,当我点击确定时什么也不做(这部分也可以正常工作)。

  • 当一个项目未被选中并且已经在 selectedItems 数组列表中时,当我单击确定时将其从数组列表中删除(这部分不起作用,让我抓狂)。

我的问题是,为什么从未调用以下代码行:

Log.d("test", "Remove is called");  
selectedItems.remove(nameItems.get(numCheckedArray.get(i)));

我不明白为什么这里的逻辑不起作用。

谢谢!

【问题讨论】:

    标签: java android arrays if-statement arraylist


    【解决方案1】:

    因为您在取消选中时删除未选中的项目(甚至在您按下确定之前)

    在这个区块

    else if (numCheckedArray.contains(which)) {
                                    //remove the position that the user clicked on
                                    numCheckedArray.remove(Integer.valueOf(which));
                                }
    

    所以当您检查此checkedItems[numCheckedArray.get(i)] 时,所有项目都是正确的,因为您删除了未检查的项目

    如果你想执行Log.d("test", "Remove is called");注释else部分numCheckedArray.remove(Integer.valueOf(which));

    【讨论】:

    • 谢谢这解决了问题,但现在我如何从 numCheckArray 中删除未选中的位置?将 numCheckedArray.remove 放在从 selectedItems 数组中删除项目的部分似乎会导致错误。
    • @topgun741 如果它解决了你的问题,你应该首先接受它作为答案:P
    • 嘿嘿酷。现在你什么时候向那个数组添加项目。如果你不添加它们,那么你不需要删除它们。如果你在检查时添加它们并点击确定,下次如果您取消选中它们,然后您可以在按钮单击后检查状态并删除未选中的状态一次
    • 在用户按下 OK 之前添加和删除并不是最好的方法。让用户选择何时出现对话框..当时不要进行更改..但是在用户按下确定后,您始终可以获取状态..然后您可以采取行动。点击确定循环并检查哪些项目是真的哪些是假的。刷新你的列表并只添加真实的,那么你的列表中将永远有检查项目
    • 是的,你的建议帮助我解决了这个问题,它完美无缺!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多