36

我试图弄清楚如何Loaders在 Android 3.0 中使用,但似乎无法让它工作。文档仅描述 usingCursorLoader但我正在使用AsyncTaskLoader.

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

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

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

编辑:似乎调用forceLoad()从返回initLoader()的加载程序至少开始加载,但这意味着您无法正确处理旋转:(

4

3 回答 3

13

Dianne Hackborn 在 bug tracker 上回复了我们,并推荐我们参考静态库实现。CursorLoader 正在执行 forceLoad() 这就是它工作的原因。

请参阅我的附加类,了解在错误跟踪器的最简单情况下为您处理此问题的类:http ://code.google.com/p/android/issues/detail?id=14944

于 2011-03-27T10:16:38.680 回答
1

您需要覆盖 onStartLoading() 方法。查看开发者网站上的示例。

    /**
     * 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();
        }
    }
于 2012-06-23T02:48:51.557 回答
0

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

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

于 2011-03-03T18:37:50.423 回答