android中使用CursorLoader代替startManagingCursor,数据库访问
我是 Android 开发新手,对新的 API 有点困惑,我似乎找不到教程来提供帮助。
我有一个带有 SQLOpenHelper 的数据库和一个数据库适配器,我在许多示例中都看到过它们,例如 这个。我想将数据连接到一个列表中,因此创建了一个 ListFragment。我看到的教程在 Activity 中使用 startManagingCursor(c) 方法,但是文档说该方法已被弃用并使用 CursorLoader。
要使用 CursorLoader,看起来我需要一个 uri,这意味着我需要一个内容提供程序。我的应用程序不需要内容提供程序,因此我不确定如何实现此功能或正确/推荐的方法是什么。
朝正确的方向推进会很棒!
I'm new to android development and I've got a little stuck with the new API, I can't seem to find a tutorial to help.
I've got a database with a SQLOpenHelper and a Database adapter that I've seen used in many examples, such as this. I want to hook up the data into a list, so have created a ListFragment. The tutorials that I've seen use the startManagingCursor(c) method in Activity, however the documentation says that this is depreciated and to use CursorLoader.
To use the CursorLoader it looks like I need a uri, which implies I need a content provider. I don't need a content provider for my app, so I'm not sure how to implement this or what is the correct/ recommended way.
A shove in the right direction would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我需要在此处添加一些要点
CursorLoader
而不是startManagingCursor
。使用方便,更安全Some points that I need to add here
CursorLoader
instead ofstartManagingCursor
. It is easy to use and more safe根据我的阅读和理解,Android 团队鼓励使用位于数据库前面的
ContentProvider
。正如您在CursorLoader
中看到的,Android API 也鼓励这种使用模式。抛开讨论这是否是小型应用程序的最佳方法,我认为您不应该与 API 作斗争而选择 ContentProvider。 CursorLoader 为您处理很多事情,我发现它工作得非常好。
From what I've read and understood, the Android team encourages the use of a
ContentProvider
that sits in front of your database. As you can see withCursorLoader
, the Android API is also encouraging this usage pattern.Letting aside discussions if this is the best approach for small apps, I think you should not fight the API and go with a ContentProvider.
CursorLoader
handles a lot of stuff for you and I find it works really well.是的,使用 ContentProvider,这是 Google 团队鼓励这样做的。记住数据库书中的三层:存储、逻辑、应用。内容提供者充当逻辑。
Yes, use ContentProvider, that's Google teams encounge to do. Remember the three layers in database book:storage, logic,application. Contentprovider act as the logic.