Android对话框setMultiChoiceItems / arraylist删除不起作用

时间:2017-01-13 00:28:01

标签: java android arrays if-statement arraylist

这是我的代码:

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 arraylist中时,当我点击确定时将它添加到arraylist(这部分工作正常)。

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

  • 当一个项目未选中且已经在selectedItems arraylist中时,当我点击确定时将其从arraylist中删除(这部分不起作用并让我疯了)。

我的问题是,为什么永远不会调用以下代码行:

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

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

谢谢!

1 个答案:

答案 0 :(得分:1)

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

在这个块中

$00...

因此,当您检查此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");