title: Android ListView with RadioButton in singleChoice mode and a custom row layout
body: <p>I have a <strong>ListView</strong>, which is in <strong>single-choice mode</strong>. All I want is to display a RadioButton to the side, that when clicked highlights to say it is selected, and when a different one is clicked that one goes back to unselected and the new one becomes selected. Why is this so hard? This should not be this complicated. I've spent <strong>DAYS</strong> looking for an appropriate answer to this and I have found nothing, so I'm asking hopefully clearly and concisely.</p>
  <p>My layout for the listview <strong>(R.layout.view_orders)</strong>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;ListView 
          android:choiceMode=&quot;singleChoice&quot;
          android:id=&quot;@android:id/list&quot;
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;fill_parent&quot;
          android:divider=&quot;@drawable/list_divider&quot;
          android:dividerHeight=&quot;1px&quot;
          android:focusable=&quot;false&quot;
          android:focusableInTouchMode=&quot;false&quot;
          android:cacheColorHint=&quot;#00000000&quot;&gt;
  &lt;/ListView&gt;
  </code></pre>
  <p>My custom row <strong>(R.layout.orders_row)</strong>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      
  &lt;RelativeLayout
      xmlns:app=&quot;http://schemas.android.com/apk/res/com.xxx.xxxxxx&quot;
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;fill_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;horizontal&quot;
      android:padding=&quot;6dip&quot;&gt;
      
      &lt;com.xxx.xxxxxx.VerticalLabelView
          app:text=&quot;SHORT&quot;
          app:textColor=&quot;#666&quot;
          app:textSize=&quot;14sp&quot;
          android:id=&quot;@+id/state&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentLeft=&quot;true&quot;
          android:layout_centerVertical=&quot;true&quot; /&gt;
      
      &lt;TextView
          android:id=&quot;@+id/quantity&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_toRightOf=&quot;@id/state&quot; 
          android:layout_centerVertical=&quot;true&quot;
          android:gravity=&quot;center&quot;
          android:textSize=&quot;40sp&quot;
          android:layout_margin=&quot;2dip&quot;
          android:minWidth=&quot;30dip&quot;
          android:textColor=&quot;#555&quot; /&gt;
          
          
      &lt;RelativeLayout
          android:layout_toRightOf=&quot;@id/quantity&quot;
          android:layout_centerVertical=&quot;true&quot;
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
          
          &lt;TextView
              android:id=&quot;@+id/instrument&quot;
              android:layout_width=&quot;fill_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:textSize=&quot;20sp&quot;
              android:textColor=&quot;#333&quot;
              android:layout_marginLeft=&quot;2dip&quot;
              android:layout_marginRight=&quot;2dip&quot;
              /&gt;
              
              
          &lt;TextView
              android:id=&quot;@+id/deets&quot;
              android:layout_below=&quot;@id/instrument&quot;
              android:layout_width=&quot;fill_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:textSize=&quot;16sp&quot;
              android:textColor=&quot;#888&quot;
              android:layout_marginLeft=&quot;2dip&quot;
              android:layout_marginRight=&quot;2dip&quot;
              /&gt;
          
      &lt;/RelativeLayout&gt;
      
          &lt;RadioButton
              android:id=&quot;@+id/selector&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_alignParentRight=&quot;true&quot;
              android:layout_centerVertical=&quot;true&quot;
              /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>My <strong>onCreate()</strong> method:</p>
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.view_orders);
      client = new Client(handler);
      ola = new OrdersAdapter(this, R.layout.orders_row, Orders);
      setListAdapter(ola);
      final RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);
      panel = (PositionsPanel) findViewById(R.id.panel);
      Utility.showProgressBar(loading);
      client.Connect(&quot;orders&quot;);
  }
  </code></pre>
  <p>Now everything underlying works as expected, you click on a radiobutton, and through its tag, I can appropriately select that item from the list and manipulate it how I want. However, when the first radio button is clicked, the last one will be selected. Click that same radio button again, and it is now selected as well. Click it once more and nothing happens, both the last and the first are selected. Now I click any other one on the list, it gets selected like expected. Click any one of the selected radio buttons and nothing happens, the radio button remains selected.</p>
  <p>I have tried using the following in <strong>onCreate()</strong>:</p>
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.view_orders);
      client = new Client(handler);
      ola = new OrdersAdapter(this, android.R.layout.simple_list_item_single_choice, Orders);
      setListAdapter(ola);
      final RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);
      panel = (PositionsPanel) findViewById(R.id.panel);
      Utility.showProgressBar(loading);
      client.Connect(&quot;orders&quot;);
  }
  </code></pre>
  <p>and that just shows no radio buttons at all. <strong>AWESOME</strong>.</p>
  <p>Now maybe (read: most likely), I'm just dense and can't figure this out, but I've seen this question asked a lot with no real answer. Lots of references to other tutorials or the Commonsware guy's book. However, the comments are old now and his repository has changed so much, that those are no longer correct answers.</p>
  <p>So, does anyone have any idea how to get the expected functionality out of this? Or failing that, just pass me along with the Gmail app's source code. :)</p>