没有SQL的ContentProvider [英] ContentProvider without SQL

查看:100
本文介绍了没有SQL的ContentProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个需要从外部应用程序访问并存储的数据.根据文档,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.

我知道没有什么规定ContentProvider必须在下面使用数据库来存储数据,但是还有其他方法可以存储必须共享给文件系统的最小数据量吗?

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?

推荐答案

我只是使用MatrixCursor解决了确切的问题.看看吧.

I just used MatrixCursor to solve that exact problem. Take a look at it.

对不起,我回答问题时刚刚扫描了问题. ContentProvider是应用程序共享数据的唯一方法.应用程序内的活动可以使用SharedPreferences来处理较小的数据,并增加持久性.

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.

编辑v2:这是我用来填充MatrixCursor的函数,用于我的ContentProvider的查询功能.我使用的是我创建的预定义VIEW,该VIEW返回一组不同的值,这将使用_ids设置光标,然后将其传递给ExpandableListView.

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;
    }

这篇关于没有SQL的ContentProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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