title: Android Gridview scrolling up and down quickly messes up the view
body: <p>I've built a custom adapter for a GridView by extending baseadapter. Everything works fine - except that if you scroll quickly up, then down, then up, then down through the gridview it eventually messes up the screen (first one icon falls out of alignment, and then when scrolling up everything simply disappears). Here is the code for my adapter - I'm using the viewholder method, which is supposed to be the best way of doing this - but it still refuses to work. I understand there's some issues in gridview with recycling of rows - how can I solve this so that no matter how many times the user scrolls up and down the grid stays in good condition with all the icons in the correct order?</p>
  
  <p>Many many thanks.</p>
  
  <p><strong>edited to show screenshots of the problem</strong></p>
  
  <p>The first screenshot shows what it looks like the first time you scroll down (there are 11 rows in the gridview and this shows the bottom row). The second screenshot shows what happens after scrolling to the top then back down to the bottom a random number of times - it starts to go out of alignment. The third screenshot shows what happens when you scroll up to the top from the messed up bottom alignment - it goes right past the top row and finishes with a totally black screen (at this point the only way out is to press 'back'). </p>
  
  <p>The data that is being displayed in the gridview is a list of custom objects - each object contains the elements ImageView and TextView for every Intent of a certain type on the device. </p>
  
  <pre><code>public class CSGridAdapter extends BaseAdapter
  {
  private final List&lt;CS&gt; csList;
  private final Context context;
  
  public CSGridAdapter(Context context, List&lt;CS&gt; csList) {
      super();
      this.csList=csList;
      this.context=context;
  }
  
  @Override
  public int getCount() {
      return csList.size();
  }
  
  @Override
  public Object getItem(int position) {
      return position;
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  private class ViewHolder {
      ImageView icon;
      TextView label;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      final ViewHolder holder;
  
      if (convertView == null) {
          holder  = new ViewHolder();
  
          final LayoutInflater mInflater = LayoutInflater.from(context);
          convertView=   mInflater.inflate(R.layout.cs_grid_item, parent, false);
  
          holder.icon = (ImageView) convertView.findViewById(R.id.cs_icon);
          holder.label= (TextView)     convertView.findViewById(R.id.cs_label);
  
          convertView.setTag(holder);
      } else {
          holder = (ViewHolder) convertView.getTag();
      }
  
      holder.icon.setTag(convertView);
      holder.label.setTag(convertView);
  
      CS cs=csList.get(position);
  
      if (cs!=null) {
          holder.icon.setImageBitmap(cs.getBitmap());
          holder.label.setText(cs.getLabel());
      }
  
      return convertView;
  }
  
  }
  </code></pre>
  
  <p><strong>edited to add layout for a single item in the gridview</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:paddingBottom="30dp"
  android:gravity="bottom|center_horizontal"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"&gt;
  
  &lt;ImageView
      android:id="@+id/cs_icon"
      android:layout_width="wrap_content"
      android:layout_height="50dp"
      /&gt;
  
  &lt;TextView
      android:paddingTop="10dp"
      android:id="@+id/cs_label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="bottom|center_horizontal"
      /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>edited to show what the gridview element looks like in the main xml</strong></p>
  
  <pre><code>  &lt;GridView
      android:paddingTop="20dp"
      android:id="@+id/gridCS"
      android:numColumns="3"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/black"
      android:overScrollMode="never"
      /&gt;
  </code></pre>
  
  <p><strong>edited to show how I added the adapter to the gridview in the main activity class</strong></p>
  
  <pre><code> CSGridAdapter csGridAdapter = new     CSGridAdapter(this.getApplicationContext(),csListToUse);
      gridView.setAdapter(csGridAdapter);
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/HEglm.png" alt="View looks ok on startup"></p>
  
  <p><img src="https://i.stack.imgur.com/UQ75O.png" alt="On scroll down - view Begins to be messed up"></p>
  
  <p><img src="https://i.stack.imgur.com/6vKnN.png" alt="On scroll up - scrolls past top row and shows only black"></p>