• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to select item in alert dialog

skyaiee

Lurker
Hi,
I've seen several examples online showing an alert dialog with radio button options combined with regular buttons like this:

Screen-shot-2010-07-18-at-6.40.08-PM.png


I want to do something very similar with two radio button options: Rider, Driver and a "Continue" and "Cancel" Button. This is my code:

final String roles[] = {"Rider","Driver"};

AlertDialog.Builder identifyRole=new AlertDialog.Builder(MainMenu.this);
identifyRole.setTitle("Are you a ...");
identifyRole.setSingleChoiceItems(roles, 0,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (whichButton ==0){
// Rider is selected
}
else{
//Driver is selected
}
}
})
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// depending on whether Rider/Driver selected, launch new activity
// how do you tell whether rider/driver is selected???
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

}
});
identifyRole.show();
}

What I can't seem to figure out from the online tutorials is how to tell which item (Rider/Driver) is selected when I click on "Continue"? I know "whichButton" will tell me that value in the OnClick method in the setSingleChoiceItems, but I don't think that value carries over into the .setPostiveButton OnClick method. It's probably a simple answer, but I've been stuck on it for a few days now. Would appreciate help! Thanks!
 
Back
Top Bottom