本文介绍了从AsyncTaskLoader更新用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经转换我的的AsyncTask AsyncTaskLoader (主要是处理配置更改)。我有一个的TextView 我使用的是一个进步的状态,并使用 onProgressUpdate 的AsyncTask 更新它。它看起来并不像 AsyncTaskLoader 有一个等价的,所以在 loadInBackground (在 AsyncTaskLoader )我使用的是这样的:

I've converted my AsyncTask to an AsyncTaskLoader (mostly to deal with configuration changes). I have a TextView I am using as a progress status and was using onProgressUpdate in the AsyncTask to update it. It doesn't look like AsyncTaskLoader has an equivalent, so during loadInBackground (in the AsyncTaskLoader) I'm using this:

getActivity().runOnUiThread(new Runnable() {
    public void run() {
        ((TextView)getActivity().findViewById(R.id.status)).setText("Updating...");
    }
});

我在片段利用这一点,这就是为什么我使用 getActivity()。这项工作pretty的好,除非配置的变化情况,如改变屏幕方向。我的 AsyncTaskLoader 继续运行(这就是为什么我使用了 AsyncTaskLoader ),但 runOnUiThread 似乎得到跳过。

I am using this in a Fragment, which is why I'm using getActivity(). This work pretty well, except when a configuration change happens, like changing the screen orientation. My AsyncTaskLoader keeps running (which is why I'm using an AsyncTaskLoader), but the runOnUiThread seems to get skipped.

不知道为什么它被忽略,或者如果这是更新从UI的最佳途径 AsyncTaskLoader

Not sure why it's being skipped or if this is the best way to update the UI from an AsyncTaskLoader.

更新:

我最终回复到一​​个的AsyncTask ,因为它似乎更适合于用户界面的更新。希望他们能合并是什么在起作用与的AsyncTask AsyncTaskLoader

I ended up reverting back to an AsyncTask as it seems better suited for UI updates. Wish they could merge what works with an AsyncTask with an AsyncTaskLoader.

推荐答案

它实际上可能。你基本上需要继承了 AsyncTaskloader 和实施 publishMessage()的方法,这将使用处理程序交付进度信息,以实现 ProgressListener (或任何你想将它命名)接口的任何类。

It's actually possible. You essentially need to subclass the AsyncTaskloader and implement a publishMessage() method, which will use a Handler to deliver the progress message to any class that implements the ProgressListener (or whatever you want to call it) interface.

下载了一个例子: http://www.2shared.com/file/ VW68yhZ1 / SampleTaskProgressDialogFragme.html (如果它下线给我发短信) - 这是基于的http:// habrahabr.ru/post/131560/

Download this for an example: http://www.2shared.com/file/VW68yhZ1/SampleTaskProgressDialogFragme.html (message me if it goes offline) - this was based of http://habrahabr.ru/post/131560/

这篇关于从AsyncTaskLoader更新用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 19:50