本文介绍了如何从 managedQuery 过渡到 LoaderManager/CursorLoader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个面向 API 级别 8(2.2,Froyo)的 Android 应用程序.我正在使用 ContentProvider ,这很简单,我使用 SimpleCursorAdapter 来填写我的列表视图,但我在文档中注意到 SimpleCursorAdapter 不推荐使用无标志构造函数,并附有以下说明:

I'm developing an Android application that is targeting API level 8 (2.2, Froyo). I'm using a ContentProvider and that's simple enough, and I'm using SimpleCursorAdapter to fill out my list view, but I noticed in the documentation for SimpleCursorAdapter that the flagless constructor is deprecated with the following note:

因为我的目标是 API 级别 8,所以 LoaderManager 没有绑定到 Activity.兼容包中的 FragmentActivity 类就是这样做的,但我没有使用 Fragments.

Since I'm targeting API level 8, a LoaderManager isn't tied to an Activity. The FragmentActivity class in the compatibility package does this, but I'm not using Fragments.

我的问题是:我应该如何在面向 11 之前 API 级别的应用程序中使用 LoaderManager/CursorLoader?我是被迫转换到 Fragments 还是应该恢复到已弃用的 SimpleCursorAdapter 构造函数(但使用 AsyncTask 使其保持 UI 线程友好,这就是CursorLoader 应该做什么)?

My question is: how exactly should I be using LoaderManager/CursorLoader in an app targeting a pre-11 API level? Am I forced to transition to Fragments or should I just revert back to the deprecated SimpleCursorAdapter constructor (but make use of an AsyncTask to keep it UI thread friendly, which is what the CursorLoader is supposed to do)?

推荐答案

我在此博文.检查一下,如果有帮助,请告诉我!:)

I've written fairly extensively about the LoaderManager in this blog post. Check it out and let me know if its helpful! :)

绝对,绝对,绝对要使用 LoaderManager.CursorLoader 类卸载了在线程上加载数据的工作,并在短期活动刷新事件(例如方向更改)期间保持数据持久.除了执行初始查询之外,CursorLoader 会向您请求的数据集注册一个 ContentObserver 并在数据集出现时调用 forceLoad()更改,因此会自动更新.这非常方便,因为您不必担心自己执行查询.当然,可以使用 AsyncTask 来保持您的应用程序 UI 线程友好,但它会涉及更多的代码......并实现您的类,例如,它会保留在 Activity 上加载 Cursor 不会简单.底线是 LoaderManager/Loader 会自动为你做这件事,并根据 Activity 正确创建和关闭 Cursor代码>生命周期.

Definitely, definitely, definitely go with LoaderManager. The CursorLoader class offloads the work of loading data on a thread, and keeps the data persistent during short term activity refresh events, such as an orientation change. In addition to performing the initial query, the CursorLoader registers a ContentObserver with the dataset you requested and calls forceLoad() on itself when the data set changes, and is thus auto-updating. This is extremely convenient as you don't have to worry about performing queries yourself. Of course it is possible to make use of AsyncTask to keep your application UI thread friendly, but it will involve a lot more code... and implementing your class so that it will, for example, retain the loaded Cursor over Activity won't be simple. The bottom line is that LoaderManager/Loader will do this automatically for you, as well as taking care of correctly creating and closing the Cursor based on the Activity lifecycle.

要在面向 11 之前 API 级别的应用程序中使用 LoaderManager/CursorLoader,只需使用兼容包中的 FragmentActivity 类.FragmentActivity 只是一个 Activity 并且是为 Android 兼容性支持而创建的,不需要在您的应用程序中使用 Fragment .只需使用 getSupportLoaderManager() 而不是 getLoaderManager() 就可以了.当然,您可以为每个屏幕实现一个父 FragmentActivity 并让它在 Fragment 中显示其布局(通过使用 FragmentActivity.getSupportFragmentManager() 在 Activity 的 onCreate() 方法中).如果您决定针对平板电脑优化应用程序,则此设计可能会使向多窗格布局的过渡更容易.这也是一次不错的学习经历:)

To use LoaderManager/CursorLoader in an app targeting a pre-11 API level, simply use the FragmentActivity class in the compatibility package. A FragmentActivity is just an Activity and has been created for Android compatibility support, and does not require the use of Fragments in your application. Just use getSupportLoaderManager() instead of getLoaderManager() and you should be all set. You could, of course, implement a parent FragmentActivity for each screen and have it displays a its layout in a Fragment (by making use of FragmentActivity.getSupportFragmentManager() in the Activity's onCreate() method). This design might make the transition to multi-pane layouts easier if you ever decide to optimize your application for tablets. It's a nice learning experience too :).

这是一个非常好的教程太.尝试通过它,如果您有任何其他问题,请随时发表评论.

This is a pretty nice tutorial too. Try and work your way through it and don't hesitate to leave a comment if you have any other questions.

这篇关于如何从 managedQuery 过渡到 LoaderManager/CursorLoader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!