Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android对话框setMultiChoiceItems/arraylist删除不工作_Java_Android_Arrays_If Statement_Arraylist - Fatal编程技术网

Java Android对话框setMultiChoiceItems/arraylist删除不工作

Java Android对话框setMultiChoiceItems/arraylist删除不工作,java,android,arrays,if-statement,arraylist,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

这是我的密码:

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();
            }
        });
}
我不明白为什么逻辑在这里不起作用


谢谢大家!

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

在这个街区

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)注释else部分
numCheckedArray.remove(Integer.valueOf(which))

谢谢这解决了问题,但现在如何从numCheckArray中删除未选中的位置?将numCheckedArray.remove放在从selectedItems数组中删除项的部分似乎会导致错误。@topgun741如果它解决了您的问题,您应该首先接受它作为答案:PHehe cool。现在您什么时候向该数组添加项。如果您不添加项,则不需要删除它们。如果您在选中并单击“确定”和“确定”时添加项,则不需要删除它们下一次,如果您取消选中它们,那么您可以在单击按钮后检查状态,并在用户按“确定”之前将其删除。让用户在对话框出现时选择。。此时不要进行更改。。但在用户按下ok后,您始终可以获得状态。。然后你可以采取行动。点击ok循环后,检查哪些项目是正确的,哪些是错误的。刷新您的列表并只添加正确的项目,那么您的列表中将始终包含检查项目是的,您的建议帮助我找到了答案,它工作得非常完美!非常感谢。
else if (numCheckedArray.contains(which)) {
                                //remove the position that the user clicked on
                                numCheckedArray.remove(Integer.valueOf(which));
                            }