Android:在单独的线程中异步运行方法
我有一种通过重新设置列表视图的适配器来刷新列表视图的方法。设置后,适配器会下载每个列表项的缩略图。
我想在新线程上运行这个名为refreshListView()的方法,以便异步下载图像,但我知道我无法在单独的线程上更新UI。
有哪些替代方法可以做到这一点?
提前致谢。
I have a method which refreshes a list view by re-setting the adapter of the list view. The adapter downloads a thumbnail image for each list item when it is set.
I want to run this method called refreshListView() on a new thread so that the images get downloaded asynchronously but i'm aware that i can't update the UI on a separate thread.
What are the alternative ways to do this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 AsyncTask ( http://developer.android.com/reference/android /os/AsyncTask.html)或加载器(http://developer.android.com/guide/topics/fundamentals/loaders.html)。我建议使用加载程序,因为它似乎总是更容易。两者(除其他外)都是针对此类问题而设计的。
为了在低于3.0的任何目标平台上使用Loader,请参考http:// developer.android.com/sdk/compatibility-library.html。
You could use an AsyncTask ( http://developer.android.com/reference/android/os/AsyncTask.html) or a Loader ( http://developer.android.com/guide/topics/fundamentals/loaders.html). I recommend using a loader as it seems to be always easier. Both were (among other things) designed for exactly this type of problem.
In order to use the Loader on any target platform less than 3.0, please refer to http://developer.android.com/sdk/compatibility-library.html.
这是完整的代码。
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/559781#559781
Here is the entire code.
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/559781#559781
AsyncTask 还适用于协调后台作业与 UI 元素更新:
http://developer.android .com/reference/android/os/AsyncTask.html
AsyncTask also works for coordinating background jobs with UI element updates:
http://developer.android.com/reference/android/os/AsyncTask.html
唔。如果我理解正确的话,您不想在图像下载完成之前使用新的适配器吗?适配器中的位置是您的图像下载代码。根据这一点,您可能只能在加载/显示适配器的视图后才能获取下载代码。
我通常首先在后台线程中下载图像,并将它们缓冲在(软引用)集合中。然后,我让适配器在加载时从该集合中读取。
Hmm. If I understand right, you don't want to use the new adapter before the images have finished downloading? Where in the adapter is your image download code. Depending on that, you might only get to the download code once the adapter's views are being loaded/showed.
I usually download images first, in a background thread, and buffer them in a collection (of SoftReferences). Then, I have the adapter read from that collection when it loads.
如前所述,您可以使用在另一个线程中运行的 AsyncTask,并且可以在单独的线程下载缩略图后更新 UI。根据您想要实现的方式,您还可以使用 AsyncTask 中的 onProgressUpdate() 方法在下载缩略图时进行更新。否则使用 onPostExecute 重置列表适配器。
As previously mentioned, you can use AsyncTask which runs in another thread and can update the UI once the seperate thread has downloaded the thumbnails. Depending on how you want to implement it, you could also have the thumbnails update as they are downloaded by using the onProgressUpdate() method in AsyncTask. Otherwise use onPostExecute to reset the list adapter.