【问题标题】:Async loading image in the ListView in androidandroid中ListView中的异步加载图像
【发布时间】:2013-07-29 10:28:59
【问题描述】:

我正在处理列表视图中的异步加载图像 搜索教程后,我实现了以下代码:

    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);
}

【问题讨论】:

  • new ImgAdapter().execute(entry.image_url) 就是你想要的。
  • 谢谢。但是错误消息:AsyncTask 类型中的方法 execute(ImageView...) 不适用于参数 (String)
  • entry.image_url 是字符串吗?
  • AsyncTask 期望你传入 ImageView 数组而不是字符串。

标签: android image listview android-listview android-asynctask


【解决方案1】:

试试:

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

execute() 方法接收在 doInBackground() 中声明的相同参数列表。在您的代码中,它是(通常是一个)ImageView 的列表。

您的代码期望 imageView 在其标签中包含图像的 URL(根据 (String) imageView.getTag())。

【讨论】:

    【解决方案2】:

    您可以将其称为并将 url 设为静态并将其访问到该 Asynk 函数 并将你的图像更新器称为

    新的 ImgAdapter().execute();

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多