user782104
user782104

Reputation: 13545

Async loading image in the ListView in android

I am working on async load image at list view After searching for the tutorial , I have implement the following code:

    package com.example.json;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

public class ImgAdapter extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String) imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

    private Bitmap download_Image(String url) {

        Bitmap bmp = null;
        try {
            URL ulrn = new URL(url);
            HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
            InputStream is = con.getInputStream();
            bmp = BitmapFactory.decodeStream(is);
            if (null != bmp)
                return bmp;

        } catch (Exception e) {
        }
        return bmp;
    }
}

The problem is, when I call it in my adapter, there is no execute method in the class, why is that and how to fix the problem? thanks.

if (entry.image_url != null) {
    thumbnail.setTag(entry.image_url);
    new ImgAdapter.execute(entry.image_url);

} else {
    thumbnail.setVisibility(View.GONE);
}

Upvotes: 0

Views: 383

Answers (2)

Jorge Cevallos
Jorge Cevallos

Reputation: 3678

Try:

thumbnail.setTag(entry.image_url);
new ImgAdapter().execute(thumbnail);

The method execute() receives the same list of arguments declared in doInBackground(). In your code, it's a list of (usually one) ImageView.

Your code is expecting the imageView to have the image's URL in its tag (as per (String) imageView.getTag()).

Upvotes: 1

Developer
Developer

Reputation: 6350

You can call this as and make url as a Static and access it into that Asynk function and call u r Image updater as

new ImgAdapter().execute();

Upvotes: 0

Related Questions