如何从 ManagedQuery 过渡到 LoaderManager/CursorLoader?

发布于 2024-12-27 15:31:01 字数 898 浏览 2 评论 0 原文

我正在开发一个针对 API 级别 8(2.2,Froyo)的 Android 应用程序。我正在使用 ContentProvider 这很简单,我正在使用 SimpleCursorAdapter 来填写我的列表视图,但我在 SimpleCursorAdapter 无标志构造函数是已弃用,并带有以下注释:

此构造函数已弃用。 不鼓励使用此选项,因为它会导致在应用程序的 UI 线程上执行游标查询,从而导致响应能力差,甚至导致应用程序无响应错误。作为替代方案,将 LoaderManager 与 CursorLoader 一起使用。

由于我的目标是 API 级别 8,因此 LoaderManager 未绑定到 Activity。兼容性包中的 FragmentActivity 类可以执行此操作,但我没有使用 Fragments。

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

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:

This constructor is deprecated.
This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

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.

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)?

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

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

发布评论

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

评论(1

情绪 2025-01-03 15:31:01

编辑:

我在这个 LoaderManager 的文章>博客文章。检查一下并告诉我是否有帮助! :)


原帖:

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

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

这也是一个非常好的教程 。尝试并解决它,如果您有任何其他问题,请随时发表评论。

Edit:

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


Original post:

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.

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.

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