listView setItemChecked不起作用

时间:2017-03-07 02:00:01

标签: java android listview alertdialog

单击按钮时会弹出一个多选对话框。 下图显示了它的外观。

enter image description here

这是问题所在。当我点击# Print input (by default), and append literal 'testing' after # lines that contain 'please'. $ sed '/please/ a testing' <<<$'yes\nplease\nmore' yes please testing more # Adding `-n` suppresses the default printing, so only `testing` is printed. # Note that the sequence of processing is exactly the same as without `-n`: # If and when a line with 'please' is found, 'testing' is appended *at that time*. $ sed -n '/please/ a testing' <<<$'yes\nplease\nmore' testing # Adding an unconditional `p` (print) call undoes the effect of `-n`. $ sed -n 'p; /please/ a testing' <<<$'yes\nplease\nmore' yes please testing more 时,我会使用select all以编程方式检查所有项目。那部分有效。但在此之后,当用户取消选中列表中的特定项目时,我想取消选中 listView.setItemChecked(position, true)

下面是mycode,我不知道为什么它不起作用。

select all

PS:当我打印private CharSequence[] itemList = {"select all", "one", "two", "three", "four", "five", "six"}; private boolean[] itemBooleanList = new boolean[]{false, false, false, false, false, false, false}; final AlertDialog.Builder myDialog = new AlertDialog.Builder(this); myDialog.setTitle("Select type(s)"); myDialog.setMultiChoiceItems(itemList, itemBooleanList, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) { final ListView listView = ((AlertDialog)dialog).getListView(); if(indexSelected == 0){ // if "select all" is clicked - check/uncheck all items for(int i=0; i<itemList.length; i++){ listView.setItemChecked(i, isChecked); itemBooleanList[i] = isChecked; } }else{ itemBooleanList[indexSelected] = isChecked; } // now I check if all item in boolean[] are true or false, if one of the item is false, it returns false and `select all` can be unchecked final boolean areAllTrue = areBooleanAllTrue(itemBooleanList); listView.setItemChecked(0, areAllTrue); System.out.println(Arrays.toString(itemBooleanList)); } }).show(); 时,如果用户取消选中一个项目,我确实看到该索引处的布尔值设置为false,这意味着逻辑是正确的,它只是itemBooleanList并没有做到这一点。

如果您要我上传更多代码或屏幕截图,请告诉我。

1 个答案:

答案 0 :(得分:1)

我希望以下代码可以解决问题,

        final ListView listView = ((AlertDialog)dialog).getListView();
    if(indexSelected == 0){ // if "select all" is clicked - check/uncheck all items
        for(int i=0; i<itemList.length; i++){
            listView.setItemChecked(i, isChecked);
            itemBooleanList[i] = isChecked;
        }
    }else{
        itemBooleanList[indexSelected] = isChecked;
        if(!isChecked) {
            itemBooleanList[0] = false;
            listView.setItemChecked(0, false);
        }else{
            //check whether all the items are checked otherthan 'select all'
            //if true then
            //itemBooleanList[0] = true;
            //listView.setItemChecked(0, true);
        }
    }