Android Honeycomb中的加载器

时间:2011-02-07 11:45:38

标签: android android-3.0-honeycomb android-loadermanager android-loader

我正在尝试弄清楚如何在Android 3.0中使用Loaders,但似乎无法让它发挥作用。文档仅描述使用CursorLoader,但我使用的是AsyncTaskLoader

从文档看来,您似乎只需要实现AsyncTaskLoader.loadInBackground(),但它永远不会在getLoaderManager().initLoader()之后调用,然后在回调中创建加载器。

我可以看到调试消息说Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}}所以它似乎已成功创建。

SDK中是否有可能破坏加载器,或者在创建加载器后是否需要调用某些方法? (他们在CursorLoader示例中没有这样做过。)

编辑:似乎在从forceLoad()返回的Loader上调用initLoader()至少开始加载,但这意味着您无法正确处理轮换:(

3 个答案:

答案 0 :(得分:13)

Dianne Hackborn在bug跟踪器上回复并将我们介绍给静态库实现。 CursorLoader正在执行forceLoad(),这就是它工作的原因。

在bug跟踪器的大多数简单情况下,请查看我的附加类,以便为您处理此问题:http://code.google.com/p/android/issues/detail?id=14944

答案 1 :(得分:1)

您需要覆盖onStartLoading()方法。请查看developer website上的示例。

    /**
     * Handles a request to start the Loader.
     */
    @Override protected void onStartLoading() {
        if (mApps != null) {
            // If we currently have a result available, deliver it
            // immediately.
            deliverResult(mApps);
        }

        // Start watching for changes in the app data.
        if (mPackageObserver == null) {
            mPackageObserver = new PackageIntentReceiver(this);
        }

        // Has something interesting in the configuration changed since we
        // last built the app list?
        boolean configChange = mLastConfig.applyNewConfig(getContext().getResources());

        if (takeContentChanged() || mApps == null || configChange) {
            // If the data has changed since the last time it was loaded
            // or is not currently available, start a load.
            forceLoad();
        }
    }

答案 2 :(得分:0)

亚历; 您是否尝试验证onLoadInBackground()是否甚至被调用?

onLoadInBackground():在工作线程上调用以执行实际加载。实现不应该直接传递结果,而应该从这个方法返回它们,最终最终会在UI线程上调用deliverResult(D)。如果实现需要在UI线程上处理结果,则可以覆盖deliverResult(D)并在那里执行。