我正在尝试使用动态数据构建多选 AlertDialog。
好的,一切都很好,等等,但是选择搞砸了。
这是我的代码:
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();
正如您在 LogCat 中看到的,我描述了我的问题:
** 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