Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentProvider without SQL

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?

like image 394
Shardul Upadhyay Avatar asked Apr 08 '11 21:04

Shardul Upadhyay


People also ask

How would you access data in a ContentProvider?

When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider .

What is a ContentProvider and what is it typically used for?

A content provider can be used to manage access to a variety of data storage sources, including both structured data, such as a SQLite relational database, or unstructured data such as image files. For more information on the types of storage available on Android, see Storage options, as well as Designing data storage.

How many ContentProvider Can an app have?

You can implement as many as you want, as you can see from the documentation here. To register a content provider, you need to add its corresponding <provider> tag in the Android Manifest. In most cases, however, you won't need multiple content providers. One is usually enough, as it can handle multiple tables.


2 Answers

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;
    }
like image 133
Kenneth Cummins Avatar answered Nov 15 '22 08:11

Kenneth Cummins


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

like image 36
Mark Mooibroek Avatar answered Nov 15 '22 09:11

Mark Mooibroek