How uncheck items in AlertDialog (setMultiChoiceIt

2019-01-15 13:29发布

问题:

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

回答1:

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

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


回答2:

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

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

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.



回答3:

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

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