我可以通过不同类型的参数,在Android中的AsyncTask的?可以通过、不同类型、参数、AsyncTask

2023-09-05 02:17:46 作者:专治各种不服

我想实现一个通用的,螺纹保存类,它需要一个ImageView的和URL(HTTP),其中所需的影像文件存储的RessourceId。它会下载图像,并填补了UiThread ImageView的的src。

I want to implement a generic, thread save class which takes the RessourceId of an ImageView and the Url (http) where the desired image file is stored. It'll download the image and fills the src of the ImageView in the UiThread.

我想 AsyncTask的将是我的最好的事情。不过,我注意到,我只能通过一种类型的参数来 doInBackground()方法。就像URL的数组。真的吗?什么ü建议我?

I thought AsyncTask would be the best thing for me. However I noticed that I only can pass one type of parameters to the doInBackground() Method. Like an Array of Urls. Is that true? What would u suggest me?

推荐答案

您可以添加setter方法​​的的AsyncTask 的实施,甚至定义自己的构造函数传递额外的参数

You can add setter methods to your AsyncTask implementation, or even define your own constructor to pass additional parameters.

另外,如果你的的AsyncTask 的实施是一个内部类可以访问活动的所有实例变量的活动。我preFER上面的选项自己,因为它清楚地表明该数据的任务需要。

Optionally, if your AsyncTask implementation is an inner class of an activity you can access all the instance variables of your activity. I prefer the above option myself, as it clearly indicates which data the task requires.