我可以通过不同类型的参数,在Android中的AsyncTask的? [英] Can I pass different types of parameters to an AsyncTask in Android?

查看:245
本文介绍了我可以通过不同类型的参数,在Android中的AsyncTask的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个通用的,螺纹保存类,它需要一个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.

这篇关于我可以通过不同类型的参数,在Android中的AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆