1. Check out our app, Forums for Android! Download and leave feedback here!

how to select item in alert dialog

Discussion in 'Application Development' started by skyaiee, Nov 11, 2010.

  1. skyaiee

    skyaiee New Member
    Thread Starter
    5

    Nov 11, 2010
    1
    0
    5
    Hi,
    I've seen several examples online showing an alert dialog with radio button options combined with regular buttons like this:

    [​IMG]

    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!
     

    Advertisement

Share This Page

Loading...