title: GridView Adapter.getView() does not set selected state properly
body: <p>I have a GridView that I am filling with a custom subclass of ArrayAdapter. This adapter returns buttons that I have customized to be selectable (see <a href="https://stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state">Android ImageButton with a selected state?</a>). This works so far and clicking the buttons selects them (which is visible with a selector-background).</p>
  
  <p>The problem is: I cannot set these buttons to a selected state from the beginning. They simply display unselected when I first start the View.</p>
  
  <p>I have created a simple test project to illustrate the problem:</p>
  
  <pre><code>package com.example.buttonselection;
  
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.GridView;
  
  public class MainActivity extends Activity {
  
      public class SelectButtonAdapter extends ArrayAdapter&lt;String&gt; {
  
          public SelectButtonAdapter(Context context) {
              super(context, 0);
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              String name = getItem(position);
  
              View rowView = convertView;
              if (rowView == null || !(rowView instanceof Button)) {
                  rowView = new Button(getContext());
                  ((Button)rowView).setOnClickListener(new OnClickListener() {
                      public void onClick(View button) {
                          if (button.isSelected()){
                              button.setSelected(false);
                          } else {
                              button.setSelected(true);
                          }
                      }
                  });
              }
  
              Button button = (Button)rowView;
              button.setText(name);
              button.setBackgroundResource(R.drawable.button_selection);
  
              button.setSelected(true); // this does not work
  
              return button;
          }
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          GridView grid = (GridView)findViewById(R.id.gridview);
  
          SelectButtonAdapter adapter = new SelectButtonAdapter(this);
          adapter.add("One");
          adapter.add("Two");
          adapter.add("Three");
          grid.setAdapter(adapter);
      }
  }
  </code></pre>
  
  <p>Because of this, I cannot even restore the state of the buttons that I saved with onSaveInstanceState. How can I solve or workaround this problem?</p>
  
  <p>I am grateful for any help!</p>
  
  <p>EDIT: here is my button_selection.xml, though this should be ok as selecting the buttons later works fine.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;item android:state_pressed="true"&gt;
          &lt;shape&gt;
  
              &lt;solid android:color="@color/bet_button_pressed" /&gt;
  
              &lt;stroke
                  android:width="2dip"
                  android:color="@color/white" /&gt;
  
          &lt;/shape&gt;
      &lt;/item&gt;
  
      &lt;item android:state_selected="true"&gt;
          &lt;shape&gt;
  
              &lt;solid android:color="@color/bet_button_selected" /&gt;
  
              &lt;stroke
                  android:width="2dip"
                  android:color="@color/white" /&gt;
  
          &lt;/shape&gt;
      &lt;/item&gt;
  
          &lt;item&gt;
          &lt;shape&gt;
  
              &lt;gradient
                  android:angle="90"
                  android:startColor="@color/bet_button_dark_green"
                  android:endColor="@color/bet_button_light_green"
                  android:centerX="0.5"
                  android:centerY="0.5" /&gt;
  
              &lt;stroke
                  android:width="2dip"
                  android:color="@color/white" /&gt;
  
          &lt;/shape&gt;
      &lt;/item&gt;
  &lt;/selector&gt;
  </code></pre>