不带 SQL 的 ContentProvider

发布于 2024-10-31 03:52:16 字数 268 浏览 1 评论 0原文

我有两条数据需要从外部应用程序访问并存储。根据文档,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 技术交流群。

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

发布评论

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

评论(2

梦断已成空 2024-11-07 03:52:16

我刚刚使用 MatrixCursor 来解决这个确切的问题。看看它。

编辑:抱歉,我回答问题时刚刚浏览了问题。 ContentProvider 是应用程序共享数据的唯一方式。应用程序中的活动可以使用 SharedPreferences 来处理较小的数据,并增加持久性。

编辑 v2:这是我用来填充 ContentProvider 的查询函数的 MatrixCursor 的函数。我正在使用我创建的预定义视图,它返回一组不同的值,这会设置一个带有 _ids 的游标,我可以将其传递给我的 ExpandableListView。

private Cursor getFactions() {
        MatrixCursor mc = new MatrixCursor(Model.Faction.PROJECTION);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = db.query(MODELFACTION_TABLE_NAME, new String[] { Model.Faction.FACTION }, null, null, null, null, Model.Faction.FACTION + " ASC");
        c.moveToFirst();
        do {
            mc.addRow(new Object[] { c.getPosition(), c.getString(c.getColumnIndex(Model.Faction.FACTION))});               
        } while (c.moveToNext() && ! c.isAfterLast());
        c.close();
        db.close();
        return mc;
    }

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 use SharedPreferences 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.

private Cursor getFactions() {
        MatrixCursor mc = new MatrixCursor(Model.Faction.PROJECTION);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = db.query(MODELFACTION_TABLE_NAME, new String[] { Model.Faction.FACTION }, null, null, null, null, Model.Faction.FACTION + " ASC");
        c.moveToFirst();
        do {
            mc.addRow(new Object[] { c.getPosition(), c.getString(c.getColumnIndex(Model.Faction.FACTION))});               
        } while (c.moveToNext() && ! c.isAfterLast());
        c.close();
        db.close();
        return mc;
    }
送君千里 2024-11-07 03:52:16

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 use SharedPreferences 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

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