title: Gridview Recycling issue with asynctask
body: <p>When I scroll back and forth through my <code>GridView</code>, my images aren't recycled correctly and will ultimately display the same image throughout the grid. </p>
  
  <p><img src="https://i.stack.imgur.com/xZdq1.jpg" alt="Recycling problem"></p>
  
  <p><strong>Adapter</strong></p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
      private Context mContext;
      private List&lt;String&gt; mList;
      private int mheight;
      private int mwidth;
      private Bitmap nBitmap;
  
      public ImageAdapter(Context context, List&lt;String&gt; list, int height, int width) {
          mContext = context;
          mList = list;
          mheight = height;
          mwidth = width;
      }
  
      @Override
      public int getCount() {
          return mList.size();
      }
  
      @Override
      public Object getItem(int position) {
          return mList.get(position).toString();
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView imageView;
          if (convertView == null) {
              imageView = new ImageView(mContext);
              InputStream is;
              try {
                  is = mContext.getAssets().open(mList.get(position));
                  Bitmap bm = BitmapFactory.decodeStream(is);
                  Bitmap mBitmap = Bitmap.createScaledBitmap(bm, mwidth / 3, mwidth / 3, false);
                  imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  
                  this.nBitmap = mBitmap;
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          } else {
              imageView = (ImageView) convertView;
          }
          imageView.setImageBitmap(nBitmap);
          return imageView;
      }
  }
  </code></pre>
  
  <p>i tried to decode the bitmap off the Ui thread scrolling working fast but Thumbnails getting loaded 1 by 1  when starting the app  and recycled thumbnails view get changed from orignal view and on low low memoery devices app crash while loading thumbnails from AsyncTask   here is updated codde </p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
  private Context mContext;
  private List&lt;String&gt; mList;
  private int mheight;
  private int mwidth;
  private InputStream is;
  
  public ImageAdapter(Context context, List&lt;String&gt; list, int height, int width) {
      mContext = context;
      mList = list;
      mheight = height;
      mwidth = width;
  }
  
  
  @Override
  public int getCount() {
      return mList.size();
  }
  
  @Override
  public Object getItem(int position) {
      return mList.get(position).toString();
  }
  
  @Override
  public long getItemId(int position) {
      return 0;
  }
  
  
  
  
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {
          imageView = new ImageView(mContext);
      } else {
          imageView = (ImageView) convertView;
      }
  
  
      InputStream is;
      try {
          is = mContext.getAssets().open(mList.get(position));
          Loadimage task = new Loadimage(imageView , mheight , mwidth);
          task.execute(is);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      return imageView ;
  
  }
   public class Loadimage extends AsyncTask&lt;InputStream, Void, Bitmap&gt;{
  private final WeakReference&lt;ImageView&gt; imageViewReference;
  
  
  private InputStream is = null;
  private int width;
  
  
  public Loadimage(ImageView imageView, int mheight, int mwidth) {
       imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);
       this.width=mwidth;
  
      // TODO Auto-generated constructor stub
  }
  
  @Override
  protected Bitmap doInBackground(InputStream... params) {
      is = params[0];
  
      if (is !=null) {
  
          Bitmap bitmap  = BitmapFactory.decodeStream(is);
          Bitmap nBitmap =Bitmap.createScaledBitmap(bitmap,width/3 , width/3, false);
          return nBitmap;     
      }
      return null; 
    }
   @Override
   protected void onPostExecute(Bitmap bitmap) {
      if (imageViewReference != null &amp;&amp; bitmap != null) {
          final ImageView imageView = imageViewReference.get();
          if (imageView != null) {
              imageView.setImageBitmap(bitmap);
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          }
  
  
  }
  </code></pre>
  
  <p>}</p>