ProgressDialog in ImageAdapter and Gallery modifying the Lazylist
===

I have this code that uses the LazyList code. I have a GridView and a Gallery which uses the same ImageAdapter as they receive photos from a remote server. (the ImageAdapter knows that the Gallery will come forth with the "simple" variable as true, and the GridView with simple= false, to make a difference in the code)

I want to know how and I am not too much into AsyncTask, but better I would like a working code if possible for:
1. A ProgressDialog before all the images in the GridView has load. 
2. A ProgressDialog before each image in the Gallery downloads.


ImageAdapter (the GridView/Gallery adapter)

    public class ImageAdapter extends BaseAdapter {
    private Activity activity;
    private String[] data;
    public static Boolean simple;
    public ImageLoader imageLoader;
    private static LayoutInflater inflater=null;

    public ImageAdapter(Activity a, String[] d, Boolean b) {
    activity = a;
    data=d;
    simple=b;
    imageLoader=new ImageLoader(activity.getApplicationContext());
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

    View vi=convertView;
    if(convertView==null)
    vi = inflater.inflate(R.layout.item, null);

    ImageView image=(ImageView)vi.findViewById(R.id.imageView1);

    imageLoader.DisplayImage(data[position], image);
    return vi;

    }
    public int getCount() {
    return data.length;
    }

    public Object getItem(int position) {
    return null;
    }

    public long getItemId(int position) {
    return 0;
    }

    }
 
Clasa ImageLoader:


    public class ImageLoader {

    MemoryCache memoryCache=new MemoryCache();
    static FileCache fileCache;
    private Map imageViews=Collections.synchronizedMap(new WeakHashMap());
    ExecutorService executorService;

    public ImageLoader(Context context){
    fileCache=new FileCache(context);
    executorService=Executors.newFixedThreadPool(5);

    }

    final int stub_id = R.drawable.stub;
    public void DisplayImage(String url, ImageView imageView)
    {
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null)
    imageView.setImageBitmap(bitmap);
        else
    {
    queuePhoto(url, imageView);
    // imageView.setBackgroundResource(stub_id);
    }
    }

    private void queuePhoto(String url, ImageView imageView)
    {
    PhotoToLoad p=new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
    }

    public static Bitmap getBitmap(String url)
    {
    File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
    return b;

    //from web
    try {
    Bitmap bitmap=null;
    URL imageUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setInstanceFollowRedirects(true);
    InputStream is=conn.getInputStream();
    OutputStream os = new FileOutputStream(f);
    Utils.CopyStream(is, os);
    os.close();
    bitmap = decodeFile(f);
    return bitmap;
    } catch (Exception ex){
    ex.printStackTrace();
    return null;
    }
    }

    //decodes image and scales it to reduce memory consumption
    private static Bitmap decodeFile(File f){
        try {
    //decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(f),null,o);

    //Find the correct scale value. It should be the power of 2.
    Boolean b=(Boolean)ImageAdapter.simple;
    final int REQUIRED_SIZE;
    if(b==true)
    { REQUIRED_SIZE=420;}
    else {REQUIRED_SIZE=70;}

    int width_tmp=o.outWidth, height_tmp=o.outHeight;
    int scale=1;
    while(true){
    if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
    break;
    width_tmp/=2;
    height_tmp/=2;
    scale*=2;
    }

    //decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize=scale;
    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
    }

    //Task for the queue
    private class PhotoToLoad
    {
    public String url;
    public ImageView imageView;
    public PhotoToLoad(String u, ImageView i){
    url=u;
    imageView=i;
    }
    }
    
    class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;
    PhotosLoader(PhotoToLoad photoToLoad){
    this.photoToLoad=photoToLoad;
    }

    public void run() {
    if(imageViewReused(photoToLoad))
    return;
    Bitmap bmp=getBitmap(photoToLoad.url);
    memoryCache.put(photoToLoad.url, bmp);
    if(imageViewReused(photoToLoad))
    return;
    BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
    Activity a=(Activity)photoToLoad.imageView.getContext();
    a.runOnUiThread(bd);
    }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad){
    String tag=imageViews.get(photoToLoad.imageView);
    if(tag==null || !tag.equals(photoToLoad.url))
    return true;
    return false;
    }

    //Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;
    public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
    public void run()
    {
    if(imageViewReused(photoToLoad))
    return;
    if(bitmap!=null)
    photoToLoad.imageView.setImageBitmap(bitmap);
    else
    photoToLoad.imageView.setImageResource(stub_id);
    }
    }

    public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
    }

    }

I don't know if in the adding of a ProgressDialog are the rest of the classes required. I'm sure a lot of users have hit over this issue and didn't find a good working answer. I do look forward to hearing from you, with suggestion, if not with a working piece of code. Thank you!
Astept raspuns. Multumesc!**