android - CursorLoader &没有内容提供程序的 SQLite [英] android - CursorLoader & SQLite without Content Provider

查看:19
本文介绍了android - CursorLoader &没有内容提供程序的 SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经讨论过了,但我想问一下事情的现状.我是否必须创建一个 ContentProvider 才能将 CursorLoader 与 sqlite 数据库结合使用?

I know this has been discussed yet I wanted to ask about the current state of the matter. Do i have to create a ContentProvider to use CursorLoader in connection with a sqlite database?

我找到了

没有 ContentProvider 的 CursorLoader 使用

正如 Emmby 评论的那样,这正是我所希望的

Looks exactly what I was hoping for yet as Emmby commented

  • 用户应该注意一个限制,即它没有刷新数据更改的机制(正如加载程序应该做的那样)

所以提到了另一种解决方案

So another solution is mentioned

https://github.com/commonsguy/cwac-loaderex

再次指出一些缺点

  • 但是,要使用自动重新查询,您需要对 UI 和更新使用相同的加载程序,这限制了其在后台服务中的可用性.

当然,在使用 LoaderManager 时,我们希望获得引入它的所有好处.所以我的问题是,是否有一种方法可以将 LoaderManager 与 sqlite 数据库结合使用,而无需实现内容提供程序,但又具有它的所有好处.

Of course when using LoaderManager we want to get all the benefits for which it was introduced. So my question is if there is a way to use LoaderManager in connection with a sqlite database without having to implement a content provider yet have all the benefits of it.

谢谢

推荐答案

您在帖子中提到的两个实现都提供了 CursorLoader 的所有优点 除了在基础内容更改时接收通知的能力.

The two implementations you mention in your post both offer all of the benefits of the CursorLoader except the ability to receive notifications when the underlying content changes.

我最近对此进行了大量研究,我可以自信地告诉您,Android API 目前没有提供仅使用原始 SQLiteDatabase 执行此操作的方法(它仅提供 SQLiteDatabasecode>ContentResolver#notifyChange() 和 Cursor#setNotificationUri() 方法,用于通知在某个通知Uri下注册的所有Cursor).

I've been looking into this a lot recently and I can confidently tell you that the Android API currently does not provide a means of doing this with only a raw SQLiteDatabase (it only provides the ContentResolver#notifyChange() and Cursor#setNotificationUri() methods, which are used to notify all Cursors registered under a certain notification Uri).

也就是说,您现在的选择是:

That said, your options right now are to:

  1. 自己实现一个观察者,当内容发生变化时,它能够接收来自 SQLiteDatabase 的通知,并且能够以某种方式将这些通知转发给所有现有的 Loader在您的应用程序中.我写了一个相当广泛的博客文章如何实现 Loader 如果你想接受这个挑战,它可能会派上用场.或者...

  1. Implement an observer yourself that is capable of receiving notifications from the SQLiteDatabase when the content changes, and is somehow able to relay these notifications to all existing Loaders in your application. I wrote a pretty extensive blog post on how to implement Loaders that might come in handy if you wish to take on this challenge. Or...

使用 Mark Murphy 的 LoaderEx 库,并且仅使用他的库提供的 AsyncTask 操作进行数据库修改.请注意,他的任务之所以刷新Loader,是因为在执行插入/更新/删除后立即在Loader上调用了onContentChanged,有效地告诉 Loader 内容已经改变,它应该刷新它的数据.

Use Mark Murphy's LoaderEx library and only make database modifications using the AsyncTask operations his library provides. Note that the reason why his tasks refresh the Loader is because they call onContentChanged on the Loader immediately after the insertion/update/delete is performed, effectively telling the Loader that the content has changed and that it should refresh its data.

只需将 ContentProviderCursorLoader 一起使用,您就可以使用 ContentResolver#notifyChange() 方法来通知 CursorLoader 表示内容发生了变化.

Just use a ContentProvider with a CursorLoader and you can use the ContentResolver#notifyChange() method to notify the CursorLoader that a content change has occurred.

我正在尝试找出一个更好的解决方案,如果我找到/实施一个,我会在将来报告,但现在这些必须要做.

I'm trying to figure out a better solution, and I'll report back in the future if I ever find/implement one, but for now these will have to do.

这篇关于android - CursorLoader &没有内容提供程序的 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆