title: Android - How to show a loading image while the adapter is loading the images?
body: <p>I'm writing an android application that download images from a server but I want to show a loading image while the image is loading in the adapter.
  This is my adapter:</p>
  
  <pre><code>public class GridViewImageAdapter extends BaseAdapter {
  Context context;
  public ImageView imageView;
  private Activity _activity;
  private ArrayList&lt;String&gt; _filePaths = new ArrayList&lt;String&gt;();
  private int imageWidth;
  
  public GridViewImageAdapter(Activity activity, ArrayList&lt;String&gt; filePaths,
          int imageWidth,Context context1) {
  
      this._activity = activity;
      this._filePaths = filePaths;
      this.imageWidth = imageWidth;
      this.context=context1;
  
  }
  
  public int getCount() {
      return this._filePaths.size();
  }
  
  public Object getItem(int position) {
      return this._filePaths.get(position);
  }
  
  public long getItemId(int position) {
      return position;
  }
  
  public View getView(int position, View convertView, ViewGroup parent) {
  
      if (convertView == null) {
          imageView = new ImageView(_activity);
      } else {
          imageView = (ImageView) convertView;
      }
  
  
      imageView.setBackgroundResource(R.drawable.gallery_image);
  
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
            imageWidth));
    Log.e("Position",position+"");
      new RetreiveBitMap().execute(_filePaths.get(position));
  
  return imageView;
  }
  
  class RetreiveBitMap extends AsyncTask&lt;String,Integer,Bitmap&gt; {
  
      private Exception exception;
      protected Bitmap doInBackground(String... urls) {
  
          Bitmap bm = null;
          URL myFileUrl =null;
          BitmapFactory.Options options=new BitmapFactory.Options();
              options.inSampleSize = 1;
          try {
              myFileUrl= new URL(urls[0]);
              } catch (MalformedURLException e) {
              e.printStackTrace();
              }
          try {
  
              String encodedfilename = Base64.encodeToString(urls[0].getBytes(),0);
  
          //Controllo presenza della cache
          try {
          InputStream check = context.openFileInput(encodedfilename);
          } catch (FileNotFoundException e) {
          // Se non presente, scarico il file
  
          HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
          conn.setDoInput(true);
          conn.connect();
          InputStream is = conn.getInputStream();
           //Salvataggio cache
          FileOutputStream fos = context.openFileOutput(encodedfilename, Context.MODE_PRIVATE);
          byte[] buffer = new byte[1024];
          int len1 = 0;
          while ( (len1 = is.read(buffer)) &gt; 0 ) {
          fos.write(buffer,0, len1);
          }
  
          fos.close();
          }
  
           bm = BitmapFactory.decodeStream(context.openFileInput(encodedfilename),null,options);
  
  
          } catch (IOException e) {
  Log.e("errore download",e.getMessage());
          } 
          return bm;
  
      } 
      protected void onPreExecute() {
  
          imageView.setImageResource(R.drawable.spinner);
      }
  
      protected void onPostExecute(Bitmap imageBit) {
  
          imageView.setImageBitmap(imageBit);
      }
  }
  }
  </code></pre>
  
  <p>This adapter show the loading images but only the last of the grid change into the downloaded image and it is in the wrong position because it should be the first of the grid.</p>