title: How can I change the background color for an specific row properly in a ListView? (Android)
body: <p>I have spent some days trying to solve a problem I have with ListViews on Android. I would like to implement a single selection list box using a ListView. So, I would like to have only one row with a predefined light background color and the rest with another preselected color. The problem I have is that when I click over a specific row is another row which is highlighted and not the one I pressed. I added several messages to Log what is happening but all seems to work fine. Here is my code:</p>
  
  <pre><code>public class TryListViewActivity extends Activity {
      protected static final int NO_SELECTED_COLOR = 0xFF191919;
      protected static final int SELECTED_COLOR = 0xFF3366CC;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          ListView listView = new ListView(this);
          ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
          list.add("Option 1");
          list.add("Option 2");
          list.add("Option 3");
          list.add("Option 4");
          list.add("Option 5");
          list.add("Option 6");
          list.add("Option 7");
          list.add("Option 8");
          list.add("Option 9");
          list.add("Option 10");
          list.add("Option 11");
          list.add("Option 12");
          list.add("Option 13");
          list.add("Option 14");
          list.add("Option 15");
  
          ArrayAdapter&lt;String&gt; listAdapter = new ArrayAdapter&lt;String&gt;(
              this,
              R.layout.list_box_entry,
              list
          );
          listView.setAdapter(listAdapter);
  
          listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  
          // Set the listener
          listView.setOnItemClickListener(
              new AdapterView.OnItemClickListener() {
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                      Log.i(
                          "Log",
                          "[SingleSelectionListBox] Item clicked: position="
                          + position + ";id=" + id
                      );
  
                      // First, set all rows to be unselected
                      int counter  = parent.getCount();
                      Log.i(
                          "Log",
                          "[SingleSelectionListBox] "
                          + counter + " items found inside the parent"
                      );
                      int children = parent.getChildCount();
                      Log.i(
                          "Log",
                          "[SingleSelectionListBox] "
                          + children + " views found inside the parent"
                      );
                      for(int i=0;i&lt;children;i++) {
                          Log.i(
                              "Log",
                              "[SingleSelectionListBox] Child "
                              + i + " has message "
                              + ((TextView)parent.getChildAt(i)).getText()
                          );
                      }
  
                      // Too inefficient but for now is OK
                      for(int i=0;i&lt;children;i++)
                          parent.getChildAt(i)
                              .setBackgroundColor(NO_SELECTED_COLOR);
                      Log.i("Log",
                          "[SingleSelectionListBox] First visible position: "
                          + parent.getFirstVisiblePosition()
                      );
  
                      // Set the background color
                      TextView textView = (TextView)(parent.getChildAt(
                          position-parent.getFirstVisiblePosition()));
                      textView.setBackgroundColor(SELECTED_COLOR);
                      Log.i(
                          "Log",
                          "[SingleSelectionListBox] Text inside the "
                          + " View changing the color " + textView.getText()
                      );
                  }
              }
          );
          setContentView(listView);
      }
  }
  </code></pre>
  
  <p>Inside the resources (res/layout) I inserted a file called list_text_entry.xml with the following content</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center"
    android:textColor="#FFFFFF" android:padding="10dp" android:textSize="16sp"&gt;
  &lt;/TextView&gt;
  </code></pre>
  
  <p>For example, If I click over the "Option 11" entry when the listView has been previously scrolled down until the first row I see is "Option 4", the "Option 7" row appears as selected being the only one carrying a blue color in the background. Can someone explain what happens here to me? I post below the messages I got throw the Log.</p>
  
  <pre class="lang-none prettyprint-override"><code>[SingleSelectionListBox] Item clicked: position=10;id=10
  [SingleSelectionListBox] 15 items found inside the parent
  [SingleSelectionListBox] 11 views found inside the parent
  [SingleSelectionListBox] Child 0 has message Option 4
  [SingleSelectionListBox] Child 1 has message Option 5
  [SingleSelectionListBox] Child 2 has message Option 6
  [SingleSelectionListBox] Child 3 has message Option 7
  [SingleSelectionListBox] Child 4 has message Option 8
  [SingleSelectionListBox] Child 5 has message Option 9
  [SingleSelectionListBox] Child 6 has message Option 10
  [SingleSelectionListBox] Child 7 has message Option 11
  [SingleSelectionListBox] Child 8 has message Option 12
  [SingleSelectionListBox] Child 9 has message Option 13
  [SingleSelectionListBox] Child 10 has message Option 14
  [SingleSelectionListBox] First visible position: 3
  [SingleSelectionListBox] Text inside the View changing the color Option 11
  </code></pre>
  
  <p>As I can guess all children inside the ViewGroup as ordered from top to bottom and even when I do this in the code:</p>
  
  <pre><code>TextView textView = (TextView)(parent.getChildAt(
      position-parent.getFirstVisiblePosition()
  ));
  textView.setBackgroundColor(SELECTED_COLOR);
  </code></pre>
  
  <p>Appears the message <code>Option 11</code> but is really the <code>option 7</code> selected. Is this a bug from Android?</p>