Single choice dialog item selection

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Single choice dialog item selection

Postby Helios » Wed Feb 25, 2009 4:44 pm

Hi every one. I have implemented a single choice dialog this way:

On my strings.xml I defined the following array and strings:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <string name="titulo_pedir_tel">Ingresar número telefónico</string>
  3.  
  4.     <string name="accion_selecionar">Aceptar</string>
  5.  
  6.     <string name="accion_cancelar">Cancelar</string>
  7.  
  8.     <!-- Opciones del menu de envio -->
  9.  
  10.     <string-array name="select_dialog_items">
  11.  
  12.         <item>Enviar vía SMS</item>
  13.  
  14.         <item>Enviar vía email</item>
  15.  
  16.     </string-array>
  17.  
  18.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


and my single choice dialog is defined as it follows in my main.class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Builder builder = new AlertDialog.Builder(this);
  3.  
  4.             builder.setIcon(R.drawable.alert_dialog_icon);
  5.  
  6.             builder.setTitle(R.string.seleccionar_forma_envio);
  7.  
  8.             builder.setSingleChoiceItems(R.array.select_dialog_items, 0, new DialogInterface.OnClickListener()
  9.  
  10.             {
  11.  
  12.                 public void onClick(DialogInterface dialog, int whichButton)
  13.  
  14.                 {
  15.  
  16.                     /* User clicked on a radio button do some stuff */
  17.  
  18.                 }
  19.  
  20.             });
  21.  
  22.             builder.setPositiveButton(R.string.accion_selecionar, new DialogInterface.OnClickListener()
  23.  
  24.             {
  25.  
  26.                 public void onClick(DialogInterface dialog, int whichButton)
  27.  
  28.                 {
  29.  
  30.                     /* User clicked Yes so do some stuff */
  31.  
  32.                 }
  33.  
  34.             });
  35.  
  36.             builder.setNegativeButton(R.string.accion_cancelar, new DialogInterface.OnClickListener()
  37.  
  38.             {
  39.  
  40.                 public void onClick(DialogInterface dialog, int whichButton)
  41.  
  42.                 {
  43.  
  44.                     /* User clicked No so do some stuff */
  45.  
  46.                 }
  47.  
  48.             });
  49.  
  50.            builder.show();
  51.  
  52.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The idea of this is that the user of my app selects a item of the array an base of the selection to realize some action. My problem is that I don't know how to recognize which item of the array the user selected. How can I solve my problem? Thanks in advance.

Regards,
Helios
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Top

Re: Single choice dialog item selection

Postby jagtap.jj1 » Thu Feb 26, 2009 2:08 pm

u have to add code in following method, where "whichButton" corr array index e.g case 0 for index 0; so on.

.setSingleChoiceItems(R.array.select_dialog_items, 0, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
switch(whichButton){
case 0 :
//code for action after click on first item of array
break;
case 1 :
//code for 2 item
break;
case 2 :

break;
case 3 :

break;
:
:
:

//so on for total num of array item
}
}
})
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby Helios » Thu Feb 26, 2009 2:55 pm

Thank you very much for your answer, it worked perfectly.

Regards,
Helios
Helios
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 17, 2009 7:11 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 1 guest