Search code examples
androidsqlitemockingandroid-contentprovider

Is it possible to mock the sqlite3 Cursor in Android when no db or table exists?


I am using a ContentProvider for caching results from a web-service query. It is an HTTP request and the response content is XML. Most of the data is cached, so I simply query the DB, if not found, request from webservice, insert in DB and requery the DB. Thus the response is always a Cursor from SQLiteDatabaseHelper.

I have one result set that is not stored in the DB and since it is 100% transient, but I would like to provide the appearance of it coming from the DB's Cursor. Is there an easy way to do this? For example, if I could project it onto the cursor with a cursor.setValue("string", objectValue) or some other existing implementation.

If not, I will either bypass the DB for this content result, or stuff it into a trivial table that is constantly reused.


Solution

  • Depending on how you use it, it might not be too hard to write your own cursor class. For convenience, derive your class from AbstractCursor class which takes care of a lot of the details for you.

    You may also be able to make use of MatrixCursor.