不带 SQL 的 ContentProvider
我有两条数据需要从外部应用程序访问并存储。根据文档,ContentProviders 是唯一可能的方式,但它也提到了外部存储。 ContentProviders 实现了一个类似数据库的“接口”,对于两条数据来说使用数据库是非常不必要的。我宁愿将它们保存到文件中,但通过实现抽象方法来使用 ContentProvider 是有问题的,因为这些方法的结构为数据库查询。
我知道没有任何规定 ContentProvider 必须使用下面的数据库来存储数据,但是有没有其他方法来存储必须共享到文件系统的最少量数据?
I have two pieces of data that need to be accessed from outside applications and stored. According to documentation ContentProviders are the only possible way, but it also mentions external storage. ContentProviders implement a database-like "interface" and using a database would be extremely unnecessary for two pieces of data. I would rather save them to a file, but using a ContentProvider by implementing the abstract methods is problematic because the methods are structured as database queries.
I know that there is nothing specifying that ContentProviders must use a database underneath to store data, but is there any other way to store minimal amount of data that has to be shared to the file system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚使用
MatrixCursor
来解决这个确切的问题。看看它。编辑:抱歉,我回答问题时刚刚浏览了问题。
ContentProvider
是应用程序共享数据的唯一方式。应用程序中的活动可以使用SharedPreferences
来处理较小的数据,并增加持久性。编辑 v2:这是我用来填充 ContentProvider 的查询函数的 MatrixCursor 的函数。我正在使用我创建的预定义视图,它返回一组不同的值,这会设置一个带有 _ids 的游标,我可以将其传递给我的 ExpandableListView。
I just used
MatrixCursor
to solve that exact problem. Take a look at it.Edit: Sorry, I had just scanned the question when I answered it.
ContentProvider
is the only way for Applications to share data. Activities within an Application can useSharedPreferences
for smaller data, with the addition of persistence.Edit v2: Here's the function I used to populate a MatrixCursor for the query function of my ContentProvider. I am using a predefined VIEW I created that returns a set of distinct values, and this sets up a cursor with _ids that I can pass to my ExpandableListView.
SharedPreferences 类提供了一个通用框架,允许您保存和检索原始数据类型的持久键值对。您可以使用
SharedPreferences
保存任何原始数据:布尔值、浮点数、整数、长整数和字符串。此数据将在用户会话中持续存在(即使您的应用程序被终止)有用的链接:
http ://developer.android.com/reference/android/content/SharedPreferences.html
http://developer.android.com/guide/topics/data/data-storage.html
The
SharedPreferences
class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can useSharedPreferences
to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed)Useful links:
http://developer.android.com/reference/android/content/SharedPreferences.html
http://developer.android.com/guide/topics/data/data-storage.html