title: GridView scrolling problem on Android
body: <p>this must be somethng very simple I'm overlooking, but I have the following problem (the post is rather lengthy, but I want to provide as much info as possible :) ).</p>
  
  <p>I have a gridview in my Android application where each cell holds custom view:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
      &lt;GridView
      android:id = "@+id/photosGridView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:clickable="true" 
      android:drawSelectorOnTop="true" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:numColumns="6" 
      android:columnWidth="90dp"
      android:verticalSpacing="5dp"
      android:horizontalSpacing="5dp"
      android:stretchMode="columnWidth"     
      &gt;
  &lt;/GridView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and each cell is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;com.myapp.widgets.ImageThumbView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background = "@android:color/transparent"
    android:paddingLeft = "1dip"
    android:paddingRight = "1dip"
    android:paddingTop = "2dip"
    android:paddingBottom = "2dip"
    &gt;
  &lt;ImageView
      android:id="@+id/thumbImage"
      android:layout_width="fill_parent"
      android:layout_height = "fill_parent"
      android:src="@drawable/icon_small"
      /&gt; 
  
    &lt;RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height = "fill_parent"
      android:background = "@android:color/transparent"
     &gt;
  
    &lt;ImageView
     android:id="@+id/iconRight"
     android:layout_width="40px"
     android:layout_height = "40px"  
     android:src="@drawable/album_check"
     android:visibility="gone"
     android:layout_alignParentTop = "true"
     android:layout_alignParentRight = "true"     
     /&gt;
  
    &lt;ImageView
     android:id="@+id/iconLeft"
     android:src="@drawable/album_check"
     android:visibility="gone"
     android:layout_width = "40px"
     android:layout_height="40px"
     android:layout_alignParentTop = "true"
     android:layout_alignParentLeft = "true"     
     /&gt; 
  &lt;/RelativeLayout&gt;
  
  &lt;/com.myapp.widgets.ImageThumbView&gt;
  </code></pre>
  
  <p>My adapter looks like this:</p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
      private List&lt;String&gt; mPictures = null;
  
      public ImageAdapter(List&lt;String&gt; pictures) {
           mPictures = pictures;
      }
  
      public int getCount() {
            return mPictures != null ? mPictures.size() : 0;
      }
      public Object getItem(int position) {
            return mPictures != null ?  mPictures.get(position) : null;
      }
      public long getItemId(int position) {
            return mPictures != null ? position : -1;
      }
      @Override
      public View getView(int position,View convertView,ViewGroup parent) 
      {
          ImageThumbView i = null;
          try
              {               
              Thread.sleep(100);
  
              if (convertView == null) 
              {
                  String path = mPictures.get(position);
                  Log.d(((Integer)position).toString(), path);
                  i = addSingleView(_li, path);
                  TextView idx = (TextView) i.findViewById(R.id.caption);
                  if (idx != null)
                      idx.setText(((Integer)position).toString());
              }
              else 
              {
                  Log.d(((Integer)position).toString(), "ALREADY NOT NULL");
                  i = (ImageThumbView) convertView;
                                      // These 2 lines were added only in desperate attempt to get it working, but it makes no difference
                  String path = mPictures.get(position);
                  i.updateView(path);
              }
          }
          catch(InterruptedException ie)
          {
              ie.printStackTrace();
          }
      return i;
          }
  }
  </code></pre>
  
  <p>So initially it works properly, ie it shows first 18 images and few pixels from the next row. But when I start scrolling the gridvew, the images start to appear at random, ie after the last image I see few from the beginning and so on. Out of curiosity, I've tried few samples like this one: <a href="http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html" rel="noreferrer">http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html</a>
  ...and see the same result.</p>
  
  <p>So, am I doing something wrong? why on earth would GridView display more items than it is supposed to do? and why do items appear at the wrong positions?</p>
  
  <p>BR,
  Alex</p>