2013-03-25 32 views
0

我正在使用AsyncTask类为我的测验应用程序下载图像。这是使用附加参数调用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。

我该如何做到这一点?

在此先感谢!

P.S .:我有一个问题和四个选项在应用程序中的每一组问题。

+0

ü得到什么问题,通过使用参数化类的构造多个参数DownloadImagesTask类? – 2013-03-25 10:18:22

+0

而不是传入'ImageView'你应该传入一个自定义类型,它包含所有的参数类型,'...'表示你可以传入一个类型的数组.. – Josh 2013-03-25 10:31:02

回答

2

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

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

在的AsyncTask

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

我没有一个名为DownloadFile (字符串字符串,诠释我)在asynctask类 – kittu88 2013-03-25 11:32:46

+0

你可以请编辑我的代码更好的理解! – kittu88 2013-03-25 11:33:11

+0

@Josh这是一个错字错误。我修好了它。 – Raghunandan 2013-03-25 11:39:22

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

} 

在你的情况,你可以通过一个浏览

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

} 
相关问题