I'm a newbie android programmer and I recently followed a tutorial which shows how to create a local SQLite database and then access the database by using SQLiteDatabase.rawQuery to return a Cursor. I would like to modify my app to use CursorLoader which is apparently a better way to access the database. My problem is the CursorLoader constructor expects a URI to be given. Do I just input "file:///[path to db]"? Seems a bit messy.
A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the table is one of the arguments.
To access the data from a content provider, URI is used as a query string. Details of different parts of Content URI: content:// – Mandatory part of the URI as it represents that the given URI is a Content URI. authority – Signifies the name of the content provider like contacts, browser, etc.
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.
ContentProviders are used to abstract the database from other parts and acts as an interface between your database and UI/other classes. You must create your own ContentProvider to share your app data with other apps. ContentResolver is used to select the right ContentProvider based on the ContentUris .
The reason behind cursor loader accepting a URI is that it expects to query a contentProvider and not a raw database.
So I suggest that you create a content provider which internally uses your database, so that you can use the CursorLoader class directly.
The advantage of using a content provider is that it encapsulates better your data and you can easily leverage a lot of apis from android directly.
Here are some guides on how to create a content provider
http://developer.android.com/guide/topics/providers/content-provider-creating.html
http://thinkandroid.wordpress.com/2010/01/13/writing-your-own-contentprovider/
If you choose not to create a ContentProvider you can extend AsyncTaskLoader or the cursor loader directly to query the data the way you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With