简体   繁体   English

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

[英]How uncheck items in AlertDialog (setMultiChoiceItems)?

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;
            }
        }
    }
});

Seeing Jorgesys answer in this question I 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);

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. 你需要从i == 0开始。我不知道你的列表中有多少项。 But if you only have 3 items then 但是,如果你只有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. 只会在列表中设置1个项目以取消选中。 It will be the 3rd one that you click. 这将是您点击的第3个。 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? 或者你想取消选中所有3个? Its not very clear which you are trying to do by your question. 你不是很清楚你想要通过你的问题做什么。

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();
}
for (int i = 0; i < visitArray.length; ++i) {
                    _selections[i] = false;
                    ((AlertDialog) dialog).getListView().setItemChecked(i, false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM