Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 4.88 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. AlertDialog MultiChoice isn't giving me the correct selections
  2. new AlertDialog.Builder(this)
  3.         .setTitle("Cities")
  4.         .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
  5.  
  6.                 @Override
  7.                 public void onClick(DialogInterface dialog, int clicked, boolean selected) {
  8.                         Log.i("Database", _options[clicked] + " selected: " + selected);
  9.                 }
  10.  
  11.         })
  12.         .setPositiveButton("OK", new DialogInterface.OnClickListener(){
  13.  
  14.                 @Override
  15.                 public void onClick(DialogInterface dialog, int clicked) {
  16.                     switch(clicked) {
  17.                         case DialogInterface.BUTTON_POSITIVE:
  18.                             for( int i = 0; i < _options.length; i++ ){
  19.                                 Log.i("Database", "id: " + _values[i] + " " + _options[i] + " selected: " + _selections[i]);
  20.                             }
  21.                             break;
  22.                     }
  23.                 }
  24.         })
  25.         .create();
  26.        
  27. ** OPENED ALERTDIALOG VIA A BUTTON AND SELECTED THE FOLLOWING: **
  28. 07-12 16:06:51.347: I/Database(8034): Aveiro selected: true
  29. 07-12 16:06:53.936: I/Database(8034): Coimbra selected: true
  30. 07-12 16:07:00.116: I/Database(8034): Porto selected: true
  31.  
  32. ** AFTER PRESSING THE OK BUTTON, THIS SHOWS UP, WHICH IS CORRECT: **
  33. 07-12 16:07:02.826: I/Database(8034): id: 1 Aveiro selected: true
  34. 07-12 16:07:02.826: I/Database(8034): id: 2 Coimbra selected: true
  35. 07-12 16:07:02.826: I/Database(8034): id: 3 Porto selected: true
  36. 07-12 16:07:02.826: I/Database(8034): id: 4 Minho selected: false
  37.  
  38. ** I CLICKED THE BUTTON TO START THE DIALOG AGAIN DE UNSELECTED THE FOLLOWING: **
  39. 07-12 16:07:07.087: I/Database(8034): Coimbra selected: false
  40.  
  41. ** AFTER PRESSING THE OK BUTTON, ALL SHOWS AS FALSE. 1 AND 3 SHOULD BE TRUE: **
  42. 07-12 16:07:08.097: I/Database(8034): id: 1 Aveiro selected: false
  43. 07-12 16:07:08.097: I/Database(8034): id: 2 Coimbra selected: false
  44. 07-12 16:07:08.097: I/Database(8034): id: 3 Porto selected: false
  45. 07-12 16:07:08.097: I/Database(8034): id: 4 Minho selected: false
  46.        
  47. public class SandBoxActivity extends Activity {
  48.  
  49.     private Button testButton;
  50.  
  51.     private CharSequence[] _options = {"Aveiro", "Coimbra", "Porto", "Minho"};
  52.  
  53.     private boolean[] _selections = {true, true, true, false};
  54.  
  55.     private AlertDialog test;
  56.  
  57.     /** Called when the activity is first created. */
  58.     @Override
  59.     public void onCreate(Bundle savedInstanceState) {
  60.         super.onCreate(savedInstanceState);
  61.         setContentView(R.layout.main);
  62.  
  63.         test = new AlertDialog.Builder(this)
  64.         .setTitle("Cities")
  65.         .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
  66.  
  67.                 @Override
  68.                 public void onClick(DialogInterface dialog, int clicked, boolean selected) {
  69.                         Log.i("Database", _options[clicked] + " selected: " + selected);
  70.                 }
  71.  
  72.         })
  73.         .setPositiveButton("OK", new DialogInterface.OnClickListener(){
  74.  
  75.                 @Override
  76.                 public void onClick(DialogInterface dialog, int clicked) {
  77.                     switch(clicked) {
  78.                         case DialogInterface.BUTTON_POSITIVE:
  79.                             for( int i = 0; i < _options.length; i++ ){
  80.                                 Log.i("Database", "id: " + " " + _options[i] + " selected: " + _selections[i]);
  81.                             }
  82.                             break;
  83.                     }
  84.                 }
  85.         })
  86.         .create();
  87.  
  88.         this.testButton = (Button) findViewById(R.id.color_button);
  89.         this.testButton.setOnClickListener(new OnClickListener() {
  90.  
  91.             @Override
  92.             public void onClick(View v) {
  93.                 test.show();
  94.             }
  95.         });
  96.     }
  97. }
  98.        
  99. .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
  100.  
  101.                 @Override
  102.                 public void onClick(DialogInterface dialog, int clicked, boolean selected) {
  103.                         Log.i("Database", _options[clicked] + " selected: " + selected);
  104.                 }
  105.  
  106.         })
  107.        
  108. .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){
  109.  
  110.                 @Override
  111.                 public void onClick(DialogInterface dialog, int clicked, boolean selected) {
  112.                         _selections[clicked] = selected;
  113.                         Log.i("Database", _options[clicked] + " selected: " + selected);
  114.                 }
  115.  
  116.         })
  117.        
  118. public void onClick(DialogInterface dialog, int clicked, boolean selected)
  119. {                      
  120.                      if(!_selections[clicked])
  121.                         _selections[clicked] = true;
  122.                     else
  123.                         _selections[clicked]=false;
  124.  
  125.                         Log.i("Database", _options[clicked] + " selected: " + selected);
  126.  
  127. }