title: SetChecked RadioButton of SingleChoice ListView on Activity Launch
body: <p>I've been working on creating a single choice ListView populated by a custom ArrayAdapter (which works), except I need to set a pre-determined RadioButton in the ListView as <code>setChecked(true)</code> when the activity launches.</p>
  
  <p>I'm populating my ListView with a <code>List&lt;Server&gt;</code> servers object at inflation that contains a boolean 'default_server' used to determine which row / RadioButton should be <code>setChecked(true)</code>.</p>
  
  <p>Selecting the various ListView items after the activity has launched correctly flags the specific RadioButton as <code>setChecked(true)</code> in Single Choice mode as desired.</p>
  
  <p>My code:</p>
  
  <p><strong>ServerActivity.java</strong></p>
  
  <pre><code>public class ServersActivity extends FragmentActivity
  //FragmentActivity needed to display dialog fragment when ListView item clicked
  ...
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.server_list);
      servers = getServers();
  
      lv = (ListView) findViewById(android.R.id.list);
      lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      adapter = new ServerActivityArrayAdapter(this, R.layout.server_list_item, servers);
      lv.setAdapter(adapter);
  
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              // show dialog if not long clicked
              if (!longClicked) {
                  lv.setItemChecked(position, true);
                  showServerDialog(position);
              }
          }
      });
  }
  </code></pre>
  
  <p><strong>ServerActivityArrayAdapter.java</strong></p>
  
  <pre><code>public class ServerActivityArrayAdapter extends ArrayAdapter&lt;Server&gt; {
  
  private int layout;
  private int mSelectedPosition = -1;
  private RadioButton mSelectedRB;
  
  private static LayoutInflater inflater;
  
  public ServerActivityArrayAdapter(Context context, int layout, List&lt;Server&gt; servers) {
      super(context, layout, servers);
      ListArrays listArrays = new ListArrays(context);
      this.layout = layout;
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  private static class ServerViewHolder {
      private TextView textView;
      private RadioButton radioButton;
  
      public ServerViewHolder() {
          // EMPTY DEFAULT CONSTRUCTOR
      }
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  
      final Server server = this.getItem(position);
      final ServerViewHolder viewHolder;
  
      if (convertView == null) {
          convertView = inflater.inflate(layout, parent, false);
          viewHolder = new ServerViewHolder();
          viewHolder.textView = (TextView) convertView.findViewById(R.id.tvServerName);
          viewHolder.radioButton = (RadioButton) convertView.findViewById(R.id.rbDefault);
          convertView.setTag(viewHolder);
      } else {
          viewHolder = (ServerViewHolder) convertView.getTag();
      }
  
      // TODO: SET THE DEFAULT SERVER
  
      viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (position != mSelectedPosition &amp;&amp; mSelectedRB != null) {
                  mSelectedRB.setChecked(false);
              }
              mSelectedPosition = position;
              notifyDataSetChanged();
              mSelectedRB = (RadioButton) v;
              server.setDefaultServer(mSelectedRB.isChecked());
          }
      });
  
      if (mSelectedPosition != position) {
          viewHolder.radioButton.setChecked(false);
      } else {
          viewHolder.radioButton.setChecked(true);
          if (mSelectedRB != null &amp;&amp; viewHolder.radioButton != mSelectedRB) {
              mSelectedRB = viewHolder.radioButton;
          }
      }
  
      viewHolder.textView.setText(server.getName());
      return convertView;
      }
  }
  </code></pre>
  
  <p>Again, my <code>List&lt;Server&gt;</code> servers object is populating the ListView, the subsequent dialog popups (when a row is clicked) correctly and the RadioButtons on each row are currently functioning in Single Choice mode. I'm using <code>server.setDefaultServer(mSelectedRB.isChecked());</code> in the setOnClickListener of ServerActivityArrayAdapter to update which server has <code>default_server(true)</code>. </p>
  
  <p>Everything I've tried so far in the ServerActivityArrayAdapter seems to break the Single Choice mode requirement of the RadioButton. How can I fix this?</p>