9

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shardul Upadhyay
  • 397
  • 2
  • 12

2 Answers2

7

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;
    }
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
Kenneth Cummins
  • 407
  • 1
  • 4
  • 13
  • Well I need to share data between applications so I suppose I will need ContentProviders. The MatrixCursor does look promising because of the ability to store objects but I suppose the MatrixCursor needs to be backed by a DB somewhere yes? – Shardul Upadhyay Apr 10 '11 at 04:26
  • Not at all! I just created the MatrixCursor, and added the elements. No db needed. When you instantiate a MatrixCursor, you provide a String[] of field names or column names, then just add the datasets or rows. Where you get that data from is completely up to you. – Kenneth Cummins Apr 10 '11 at 05:28
  • 2
    Ended up using a matrix cursor with standard file input/output underneath. Then reading the information by using ContentResolver.query () and storing it in a cursor. Thanks for the help! – Shardul Upadhyay Apr 14 '11 at 20:38
1

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

Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53