我正在列表视图中处理异步加载图像搜索教程后,我实现了以下代码:
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;
}
}
问题是,当我在适配器中调用它时,类中没有执行方法,为什么会这样以及如何解决问题?谢谢。
if (entry.image_url != null) {
thumbnail.setTag(entry.image_url);
new ImgAdapter.execute(entry.image_url);
} else {
thumbnail.setVisibility(View.GONE);
}