title: Android GridView first button not working
body: <p>I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.</p>
  
  <p>Here is the code where I create the view:</p>
  
  <pre><code>public View getView(int position, View convertView, ViewGroup parent) {
      Button imageView;
  
      if (convertView == null) {  // if it's not recycled, initialize some attributes
          Log.w("NOVO", "narejena nova celica");
          imageView = new Button(mContext);
  
          imageView.setPadding(8, 8, 8, 8);
      } else {
          Log.w("STARO", "stara celica");
          imageView = (Button) convertView;
      }
  
      imageView.setEnabled(true);
  
      int visina = parent.getHeight();
      int sirina = parent.getWidth();
  
      float dip = mContext.getResources().getDisplayMetrics().density;
      float margin = 10*dip;
  
      int view_height = (int)(visina - 3*margin)/4;
      int view_width = (int)(sirina - 2*margin)/3;
  
      int view_dim = 0;
      if (view_height &lt;= view_width)
          view_dim = view_height;
      else
          view_dim = view_width;
  
      imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));
  
      imageView.setId(position);
      imageView.setOnClickListener(celice.get(position));
      /*imageView.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              //Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
              //toast.show();
              celice.get(v.getId()).celicaVisible(4000);
          }});*/
  
      celice.get(position).id = position;
      celice.get(position).setButton(imageView);
  
      return imageView;
  }
  </code></pre>
  
  <p>If I replace</p>
  
  <pre><code>imageView = (Button) convertView;
  </code></pre>
  
  <p>with</p>
  
  <pre><code>imageView = new Button(mContext);
  </code></pre>
  
  <p>then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.</p>
  
  <p>And here is the custom class "Celica" that takes care of the actual work - changing the background...</p>
  
  <pre><code>public class Celica implements OnClickListener {
  
  public boolean odkrit;
  public boolean najden;
  public int id;
  public Drawable slikca1, slikca2;
  public Celica par;
  private Timer tim;
  public Button but;
  public Context con;
  static int buttonsVisible = 0;
  
  Celica(Drawable s1, Drawable s2) {
      this.slikca1 = s1;
      this.slikca2 = s2;
  }
  
  void celicaVisible(int millis) {
      if (odkrit)
          return;
  
      Log.w("TEST", "prizganih " + buttonsVisible);
      if (buttonsVisible &gt;= 2)
          return;
  
      odkrit = true;
      buttonsVisible++;
      tim = new Timer();
      tim.schedule(new timerDone(), millis);
  
      ((Activity)con).runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 but.setBackground(slikca2);
             }
      });
  
  }
  
  void setButton(Button b) {
      but = b;
  
      ((Activity)con).runOnUiThread(new Runnable() {
          @Override
          public void run() {
             but.setBackground(slikca1);
          }
   });
  }
  
  class timerDone extends TimerTask {
  
      @Override
      public void run() {
          if (!najden) {
              odkrit = false;
  
              ((Activity)con).runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         but.setBackground(slikca1);
                     }
              });
          }
  
          buttonsVisible--;
          tim.cancel();
      }
  
  }
  
  @Override
  public void onClick(View v) {
      celicaVisible(4000);
  }
  </code></pre>
  
  <p>}</p>