java 如何取消选中 AlertDialog (setMultiChoiceItems) 中的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5067641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:15:55  来源:igfitidea点击:

How uncheck items in AlertDialog (setMultiChoiceItems)?

javaandroidandroid-alertdialog

提问by Eduardo Teixeira

I'd like to clear selected items when the total came to three items selected, I am doing as follows but is not working ...

当总数达到选定的三个项目时,我想清除选定的项目,我正在执行以下操作但不起作用......

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getText(R.string.escolhaArquivosBaixados));
builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        //                  
        int count = 0;
        for(int i = 1; i < selected.length; i++){
            //
            if (selected[i]){
                count++;
            }
            if (count == 3){
                //enter here but nothing happens
                ((AlertDialog) dialog).getListView().setItemChecked(which, false);
                break;
            }
        }
    }
});

回答by Eduardo Teixeira

Seeing Jorgesys answer in this questionI realized what was missing in my code, is necessary to change the boolean list too.

看到 Jorgesys 在这个问题中的回答,我意识到我的代码中缺少什么,也需要更改布尔列表。

        selected[which] = false;
        ((AlertDialog) dialog).getListView().setItemChecked(which, false);

回答by FoamyGuy

The first index in an array is 0, not 1. So this:

数组中的第一个索引是 0,而不是 1。所以这个:

for(int i = 1; i < selected.length; i++){
                //
                if (selected[i]){
                    count++;
                }

Is never going to check the first item in the boolean array. You need to start with i == 0. I don't know how many items are in your list. But if you only have 3 items then

永远不会检查布尔数组中的第一项。您需要从 i == 0 开始。我不知道您的列表中有多少项。但是如果你只有 3 个项目,那么

if (count == 3){

won't ever be true because its only going to check the last two in the array. Also this call:

永远不会是真的,因为它只会检查数组中的最后两个。还有这个电话:

((AlertDialog) dialog).getListView().setItemChecked(which, false); 

is only going to set 1 item in the list to unchecked. It will be the 3rd one that you click. So the first two that you click are going to get checked and stay checked. Then when you click on the third one it will get checked for a split second and then uncheck itself. Is that what you are trying to do? or do you want to uncheck all 3 of them? Its not very clear which you are trying to do by your question.

只会将列表中的 1 项设置为未选中。这将是您单击的第 3 个。因此,您单击的前两个将被检查并保持检查状态。然后当你点击第三个时,它会被检查一秒钟,然后取消选中它自己。这就是你想要做的吗?还是要取消选中所有 3 个?它不是很清楚你想通过你的问题做什么。

回答by Abdul Rizwan

if you want to use multicheckoption as single selection option then use this code.

如果您想使用 multicheckoption 作为单选选项,请使用此代码。

 String[] items = new String[]{"Most Funded (high - low)", "Most Funded (low - high)", "Newest first", "Funding Ask"};
boolean selected[] = new boolean[]{false, false, false, true};

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getResources().getText(R.string.sortby));
    builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            //
            for (int i = 0; i < selected.length; i++) {
                if (i == which) {
                    selected[i]=true;
                    ((AlertDialog) dialog).getListView().setItemChecked(i, true);
                }
                else {
                    selected[i]=false;
                    ((AlertDialog) dialog).getListView().setItemChecked(i, false);
                }
            }
        }

    });
    builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    builder.show();
}

回答by Mahalakshmi

for (int i = 0; i < visitArray.length; ++i) {
                    _selections[i] = false;
                    ((AlertDialog) dialog).getListView().setItemChecked(i, false);
}