title: Android singlechoiceitem dialog
body: <p>I have a dialog like so.</p>
  
  <pre><code>AlertDialog.Builder mySortAlertDialog = new AlertDialog.Builder(this,
      AlertDialog.THEME_DEVICE_DEFAULT_DARK);
  mySortAlertDialog.setTitle("Sort by?");
  String[] r = {
      "Firstname", "Lastname"
  };
  mySortAlertDialog.setSingleChoiceItems(r, -1, new DialogInterface.OnClickListener() {
  
      public void onClick(DialogInterface dialog, int which) {
  
      }
  });
  </code></pre>
  
  <p>This create two choices and now I need a button to confirm the users choice. So I do that like so:</p>
  
  <pre><code> mySortAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {@
           Override
           public void onClick(DialogInterface dialog, int which) {   
  </code></pre>
  
  <p>Now the problem is. I have two listeners, one to check which option is selected and the other to confirm the choice and do logic using the values from the first listener. </p>
  
  <p>My question is, how do I make these two listeners interact so that the user selects either firstname or lastname and then presses OK and then logic is performed based on the choice.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: this is what my dialog looks like...</p>
  
  <p><img src="https://i.stack.imgur.com/2c8io.jpg" alt="enter image description here"></p>