0

我正在使用 AsyncTask 类为我的测验应用程序下载图像。这是课程

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;

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

}

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

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

从我的活动中给这个班级打电话,如下所示:

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

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

我想传递一些额外的参数,以确定我是下载问题的图像还是答案 TextViews。

我如何实现这一目标?

提前致谢!

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

4

2 回答 2

2

您可以将参数传递给 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; 
}  
于 2013-03-25T10:20:39.803 回答
0

您可以将第一个参数的类型更改为对象(或创建自定义参数类/哈希 - 最佳选择)并将多个项目传递给它。

ImageView imgView = (ImageView) findViewById(R.id.imageView1);
String param = "parameter";

MyAsyncTask task = new MyAsyncTask();
task.execute(param, imgView);

private class MyAsyncTask extends AsyncTask<Object, Void, Void> {

    private String parameter;
    private ImageView imgView;

    @Override
    protected Void doInBackground(Object... params) {

        //Looping option
        for (Object object : params) {
            if (object instanceof String) {
                this.parameter = (String) object;
                //do_something
            } else if (object instanceof ImageView) {
                //do_something_else 
            }
        }

        //Direct access option

        this.parameter = (String) params[0];
        this.imgView = (ImageView) params[1];

        return null;
    }   

}

在您的情况下,您可以通过 View

new DownloadImagesTask().execute(image_downloadView, question_resultView);

private class DownloadImagesTask extends AsyncTask<View, Void, Bitmap> {

    TextView questionView = null;
    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(View... views) {
        for (View view : views) {
            if (view instanceof ImageView) {
                this.imageView = (ImageView) view;
            } else if (view instanceof TextView) {
                this.questionView = (TextView) view;
            }
        }

        return download_Image((String)imageView.getTag());

    } 

    @Override
    protected void onPostExecute(Bitmap result) {
        // imageView.setImageBitmap(result);
        downloaded_image = new BitmapDrawable(result);
        //setting question image
        questionView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, downloaded_image);
    }

}
于 2013-03-25T10:41:56.957 回答