- AlertDialog MultiChoice isn't giving me the correct selections
- 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();
- ** 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
- public class SandBoxActivity extends Activity {
- private Button testButton;
- private CharSequence[] _options = {"Aveiro", "Coimbra", "Porto", "Minho"};
- private boolean[] _selections = {true, true, true, false};
- private AlertDialog test;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- test = 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: " + " " + _options[i] + " selected: " + _selections[i]);
- }
- break;
- }
- }
- })
- .create();
- this.testButton = (Button) findViewById(R.id.color_button);
- this.testButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- test.show();
- }
- });
- }
- }
- .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int clicked, boolean selected) {
- Log.i("Database", _options[clicked] + " selected: " + selected);
- }
- })
- .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int clicked, boolean selected) {
- _selections[clicked] = selected;
- Log.i("Database", _options[clicked] + " selected: " + selected);
- }
- })
- public void onClick(DialogInterface dialog, int clicked, boolean selected)
- {
- if(!_selections[clicked])
- _selections[clicked] = true;
- else
- _selections[clicked]=false;
- Log.i("Database", _options[clicked] + " selected: " + selected);
- }