16

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;
            }
        }
    }
});
Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
Eduardo Teixeira
  • 963
  • 1
  • 11
  • 21

4 Answers4

51

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);
Community
  • 1
  • 1
Eduardo Teixeira
  • 963
  • 1
  • 11
  • 21
  • 2
    Thanks! This was driving me crazy! The workaround with the array works like a charm! – TSGames May 21 '14 at 15:28
  • 2
    Wasted a day to find this answer. – Shabbir Dhangot Sep 23 '16 at 13:10
  • 1
    Great finding, I had a hard time to find the problem. Just a note, the `selected[which] =false;` must be placed **before** the `((AlertDialog) dialog).getListView().setItemChecked(which, false);` – P1x Sep 05 '20 at 21:27
1

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();
}
Abdul Rizwan
  • 3,904
  • 32
  • 31
1

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.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Hello Tim, thanks for the reply, I'm starting the index number 1 because I ignore the first item. Yes is what i want ("when you click on the third one it will get checked for a split second and then uncheck itself.") – Eduardo Teixeira Feb 21 '11 at 16:32
  • 1
    My question is about the command,("((AlertDialog) dialog).getListView().setItemChecked(which, false); ") it runs but nothing happens. – Eduardo Teixeira Feb 21 '11 at 16:36
0
for (int i = 0; i < visitArray.length; ++i) {
                    _selections[i] = false;
                    ((AlertDialog) dialog).getListView().setItemChecked(i, false);
}
Mahalakshmi
  • 320
  • 2
  • 13