java 安卓:CursorLoader、LoaderManager、SQLite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11017646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:26:46  来源:igfitidea点击:

Android: CursorLoader, LoaderManager, SQLite

javaandroidmultithreadingandroid-loadermanager

提问by Jim

Trying to update my old app in which some methods are deprecated. I found out that if I want to work with ListView that shows data from db, I should use LoaderManager + CursorLoader. CursorLoader works with content providers. So for every table in my db I should create content provider now ? Why should I ? As far as I know content providers is used to share some db information with other applications, but my app dont share any information. So can I use CursorLoader without content providers ???

试图更新我的旧应用程序,其中某些方法已被弃用。我发现如果我想使用显示来自 db 的数据的 ListView,我应该使用 LoaderManager + CursorLoader。CursorLoader 与内容提供者一起工作。那么对于我的数据库中的每个表,我现在应该创建内容提供者吗?我为什么要 ?据我所知,内容提供程序用于与其他应用程序共享一些数据库信息,但我的应用程序不共享任何信息。那么我可以在没有内容提供者的情况下使用 CursorLoader 吗???

采纳答案by Alex Lockwood

I've written a blog poston this subject. You can also check out this answerfor more information. Hopefully it will clear things up for you.

我写了一篇关于这个主题的博客文章。您还可以查看此答案以获取更多信息。希望它会为您解决问题。

As Barak mentioned, one can implement a CursorLoaderwithout content providers by extending AsyncTaskLoader<Cursor>class. That said, most of the tutorials and sample code use ContentProviders, and it seems like the Android team encourages its use as well. It's also a lot less complicated than implementing your own class.

正如 Barak 所提到的,可以CursorLoader通过扩展AsyncTaskLoader<Cursor>类来实现一个没有内容提供者的。也就是说,大多数教程和示例代码都使用ContentProviders,而且 Android 团队似乎也鼓励使用它。它也比实现自己的类要简单得多。

That said, if you reallydon't want to use content providers, Dianne Hackborn (one of the Android framework developers, and also known as "hackbod" here on SO) suggests writing your own loader that uses your database class instead of a content provider. The easiest way is just to take the source of the CursorLoaderclass from the compatibility library, and replace provider queries with queries to your own db helper class.

也就是说,如果您真的不想使用内容提供程序,Dianne Hackborn(Android 框架开发人员之一,在 SO 上也称为“hackbod”)建议您编写自己的加载程序,使用您的数据库类而不是内容提供者。最简单的方法是CursorLoader从兼容性库中获取类的源代码,并将提供者查询替换为对您自己的 db 帮助程序类的查询。

回答by DArkO

Yes you can, You can have Custom data loaders which can load objects that you define or any object type or list in that matter.

是的,您可以,您可以拥有自定义数据加载器,它可以加载您定义的对象或任何对象类型或列表。

Just look into the samples from the android sdk for the LoaderCustomSupport.java in the compatibility library samples and demos.

只需查看兼容性库示例和演示中 LoaderCustomSupport.java 的 android sdk 示例。