Android Honeycomb 中的加载器

发布于 2024-10-16 08:22:29 字数 556 浏览 7 评论 0原文

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

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

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

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

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

I'm trying to figure out how to use Loaders in Android 3.0 but can't seem to get it to work. The docs only describe using CursorLoader but I'm using AsyncTaskLoader.

From the docs it seems that you should only need to implement AsyncTaskLoader.loadInBackground() but it never gets called after getLoaderManager().initLoader() and then creating the loader in the callback.

I can see debug messages saying Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}} so it seems like it is created successfully.

Is it possible that loaders are currently broken in the SDK or is there some method you need to call after creating the loader? (they haven't done that in the CursorLoader example).

EDIT: Seems like calling forceLoad() on the Loader returned from initLoader() starts the loading at least but this means you can't handle rotations correctly :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枉心 2024-10-23 08:22:29

Dianne Hackborn 回复了错误跟踪器,并向我们推荐了静态库实现。 CursorLoader 正在执行forceLoad(),这就是它工作的原因。

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

Dianne Hackborn replied on the bug tracker and referred us to the static library implementation. CursorLoader is doing forceLoad() which is why it is working.

See my attached class for a class which handles this for you in most simple cases at the bug tracker: http://code.google.com/p/android/issues/detail?id=14944

深爱成瘾 2024-10-23 08:22:29

您需要重写 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();
        }
    }

You need to override the onStartLoading() method. Look at the example on the 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();
        }
    }
时光磨忆 2024-10-23 08:22:29

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

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

Alex;
Did you try to validate if the onLoadInBackground () gets even called?

onLoadInBackground (): Called on a worker thread to perform the actual load. Implementations should not deliver the result directly, but should return them from this method, which will eventually end up calling deliverResult(D) on the UI thread. If implementations need to process the results on the UI thread they may override deliverResult(D) and do so there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文