title: Set default selected item of ListView Alert Dialog in Android
body: <p>I have created a dialog with a custom <code>ListView</code> that models a <code>Spinner</code> display, and originally it starts out with the value "Select Gender".</p>
  
  <p>When the dialog opens it prompts for a selection (just like a spinner). If the selection gets selected again, it shows the same options, but doesn't indicate which option has already been selected.</p>
  
  <p><strong>Example:</strong><br>
  Default Value: "Select Gender"<br>
  Dialog opens with no selection<br>
  User selects: "Male"<br>
  User reopens dialog...<br>
  Dialog opens with no selection</p>
  
  <p>(<strong>I'd like it to have "Male" selected, since that was their last selection</strong>)</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>genderItems = getResources().getStringArray(R.array.gender_array);
  genderAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_dropdown_item, genderItems);
  
  genderDrop.setOnTouchListener(new View.OnTouchListener() {
          public boolean onTouch(View v, MotionEvent event) {
              if(event.getAction() == MotionEvent.ACTION_DOWN) {
                  Builder genderBuilder = new AlertDialog.Builder(Register.this)
                      .setTitle(R.string.gender_prompt)
                      .setAdapter(genderAdapter, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                          inputGender.setText(genderItems[which]);
                          dialog.dismiss();
                      }
                  });
                  AlertDialog genderAlert = genderBuilder.create();
                  genderAlert.show();
                  genderAlert.getListView().setSelection(0);
              }
              return false;
          }
  });
  </code></pre>
  
  <p><code>genderAlert.getListView().setSelection(0)</code> <strong>doesn't set the default selected</strong> as "Male"<br>
  <code>genderAlert.getListView().setSelection(1)</code> <strong>doesn't set the default selected</strong> as "Female"</p>