Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CursorLoader, get URI for local database

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.

like image 888
user1681358 Avatar asked Sep 18 '12 21:09

user1681358


People also ask

What is content URI in Android?

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.

Which is the standard prefix used for query string URI by content provider?

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.

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.

What is content provider and content resolver?

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 .


1 Answers

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.

like image 169
Julian Suarez Avatar answered Sep 25 '22 09:09

Julian Suarez