Android Honeycomb中的加载器

36

我正在尝试在Android 3.0中使用Loader,但似乎无法使其工作。文档只描述了如何使用CursorLoader,但我正在使用AsyncTaskLoader

从文档中可以看出,你只需要实现 AsyncTaskLoader.loadInBackground() 就可以了,但是在调用 getLoaderManager().initLoader() 并在回调函数中创建loader后,该方法从未被调用过。

我可以看到调试信息中有 Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}},因此看起来它成功地被创建了。

这个SDK中的Loader是否存在问题?或者在创建loader之后需要调用某些其他方法吗? (在CursorLoader示例中没有这样做)。

编辑:调用从initLoader()返回的Loader上的forceLoad()至少会启动加载,但这意味着无法正确处理旋转 :(


如果您找到了答案,请也告诉我一声。我自己也没能找到任何信息。 - Brian Griffey
2
还有一个 http://code.google.com/p/android/issues/detail?id=14944 提到了与“编辑”评论相同的解决方法。 - typo.pl
是的,这就是我的关于这个问题的错误报告 :) - alexanderblom
1
你有看过 http://developer.android.com/reference/android/app/LoaderManager.html 吗? - David Kuridža
3个回答

13

Dianne Hackborn在错误跟踪器上回复并引用了静态库的实现。CursorLoader正在执行forceLoad(),这就是它能够工作的原因。

请参阅我在错误跟踪器上附加的类,该类可在大多数简单情况下为您处理:http://code.google.com/p/android/issues/detail?id=14944


4
他们确实需要记录CPL。> . < - Nathan Schwermann
1
真的很糟糕。因此,如果您使用支持库,则此处显示的示例:http://developer.android.com/reference/android/content/AsyncTaskLoader.html 将无法正常工作,除非您覆盖 onStartLoading - Blundell
现在我有了自己的ASyncTaskLoader参考源,使用支持库:http://blog.blundell-apps.com/tut-asynctask-loader-using-support-library/ - Blundell

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();
        }
    }

0
Alex; 你有尝试验证 onLoadInBackground() 是否被调用了吗?
onLoadInBackground(): 在工作线程上调用以执行实际加载。实现不应直接传递结果,而应从此方法返回它们,最终将在 UI 线程上调用 deliverResult(D)。如果实现需要在 UI 线程上处理结果,则可以重写 deliverResult(D) 并在那里进行处理。

1
不,除非你使用forceLoad(),否则它不会被调用。 - alexanderblom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接