java - 使用附加参数调用 AsyncTask 类以检查在何处显示图像

标签 java android parameters android-asynctask

我正在使用 AsyncTask 类下载测验应用程序的图像。这是类(class):

public class DownloadImagesTask 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);
        downloaded_image = new BitmapDrawable(result);
        //setting question image
        question_view.setCompoundDrawablesWithIntrinsicBounds(null, null, null, downloaded_image);
    }

    private Bitmap download_Image(String url) {

        //---------------------------------------------------
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
        } 
        return bm;

        //--------------------------------------------------
    }

}

图像正在下载,我将图像放入我的Question TextView中,如下所示:

downloaded_image = new BitmapDrawable(result);
//setting question image
question_view.setCompoundDrawablesWithIntrinsicBounds(null, null, null, downloaded_image);

我正在从我的 Activity 中调用此类,如下所示:

//image download
String imageURL = "http://cache.daylife.com/imageserve/074efG3gPV4oK/50x50.jpg";

image_downloadView.setTag(imageURL);
new DownloadImagesTask().execute(image_downloadView);

我想传递一些额外的参数,以便确定我是否正在下载问题或答案 TextView 的图像。

如何实现这一目标?

提前致谢!

附注:对于每组问题,我在申请中都有一个问题和四个选项。

最佳答案

您可以将参数传递给 AsyncTask 构造函数。

DownLoadImagesTask dt= new DownLoadImagesTask("hello",1); //passing string and integer value
dt.execute(image_downloadView);

在异步任务中

String s;
int value;
public DownLoadImagesTask(String string, int i) {
    // TODO Auto-generated constructor stub
    s=string;
    value=1; 
}  

关于java - 使用附加参数调用 AsyncTask 类以检查在何处显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612400/

相关文章:

java - 当分类器返回列表时,groupingBy 如何工作?

java - 正则表达式密码验证

java - 我们可以根据 GUI 组件的大小设置/调整 JTable 列的大小吗?

Android:检测时钟分钟变化的监听器

C++ 参数更改为垃圾,即使使用常量调用也是如此

java - 碰撞检测: rounded object

java - 使用 Joda 了解时间何时超过某个点

java - 无法从 AsyncTask android 中的 onPostExecute 将值设置为字符串

javascript - 论点在词汇环境中的位置在哪里?

java - JSF 2.0 中带有参数的 selectOneMenu 的 selectItems