I’m trying to build a multichoice AlertDialog using dynamic data.
Ok, it’s all loading great and etc, but the selections are messed up.
Here is my code:
new AlertDialog.Builder(this)
.setTitle("Cities")
.setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int clicked, boolean selected) {
Log.i("Database", _options[clicked] + " selected: " + selected);
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int clicked) {
switch(clicked) {
case DialogInterface.BUTTON_POSITIVE:
for( int i = 0; i < _options.length; i++ ){
Log.i("Database", "id: " + _values[i] + " " + _options[i] + " selected: " + _selections[i]);
}
break;
}
}
})
.create();
As you can see in the LogCat, I described my problem:
** OPENED ALERTDIALOG VIA A BUTTON AND SELECTED THE FOLLOWING: **
07-12 16:06:51.347: I/Database(8034): Aveiro selected: true
07-12 16:06:53.936: I/Database(8034): Coimbra selected: true
07-12 16:07:00.116: I/Database(8034): Porto selected: true
** AFTER PRESSING THE OK BUTTON, THIS SHOWS UP, WHICH IS CORRECT: **
07-12 16:07:02.826: I/Database(8034): id: 1 Aveiro selected: true
07-12 16:07:02.826: I/Database(8034): id: 2 Coimbra selected: true
07-12 16:07:02.826: I/Database(8034): id: 3 Porto selected: true
07-12 16:07:02.826: I/Database(8034): id: 4 Minho selected: false
** I CLICKED THE BUTTON TO START THE DIALOG AGAIN DE UNSELECTED THE FOLLOWING: **
07-12 16:07:07.087: I/Database(8034): Coimbra selected: false
** AFTER PRESSING THE OK BUTTON, ALL SHOWS AS FALSE. 1 AND 3 SHOULD BE TRUE: **
07-12 16:07:08.097: I/Database(8034): id: 1 Aveiro selected: false
07-12 16:07:08.097: I/Database(8034): id: 2 Coimbra selected: false
07-12 16:07:08.097: I/Database(8034): id: 3 Porto selected: false
07-12 16:07:08.097: I/Database(8034): id: 4 Minho selected: false
EDIT
Tried your code and it is working fine for me. Step through it and make sure your values are correct.
Here is my test app. The only difference is that i did not know what
_valueswas, so i took it out.OLD POST
I do not see that you are actually handling the fact that it was clicked. I see that you are printing to the log that it was clicked, but you are not changing the fact that it is selected in your
_selectionsarray. That could possibly be your problem.Try this: