I was reading the doc, but I am still not too sure. Its says to use
Check out my blog post on the subject:
CursorLoaderhas nothing to do with it.
Insertion is a totally different concept... it has absolutely nothing to do with the
CursorLoader. When coupled with the
CursorLoader automatically queries your database and updates itself when the
ContentObserver is notified of a datastore change. It has nothing to do with the actual process of inserting data into your database.
When you insert (or query or update or delete) data into your database via the content provider, you don't communicate with the provider directly. Instead, you use the
ContentResolver object to communicate with the provider (note that the
ContentResolver is a private instance variable in your application's global
Context) . More specifically, the sequence of steps performed is:
ContentResolver object determines the authority of the Uri.
ContentResolver relays the request to the content provider registered with the authority (this is why you need to specify the authority in the
The content provider receives the request and performs the specified operation (in this case
insert). How and where the data is inserted depends on how you implemented the
insert method (
ContentProvider is an abstract class that requires the user to implement
Hopefully you were able to wrap your head around that at least a little. The reason why there are so many steps involved is because Android (1) allows applications to have more than one content provider, and (2) needs to ensure that apps can securely share data with other third-party apps. (It wasn't because it wanted to confuse you, I promise).
Now that you (hopefully) have a better idea of how the
ContentResolver is able to relay these requests to the content provider, inserting the data is fairly straight forward:
First, decide which uri you want to have matched by your content provider. This depends on how you decided to match your uris with the
UriMatcher. Each uri you have represents a different means of inserting data into your internal database (i.e. if your app has two tables, you will probably have two uris, one for each table).
Create a new
ContentValues object and use it to package the data you wish to send to the content provider. The
ContentValues object maps column names to data values. In the below example, the column name is "column_1" and the value being inserted under that column is "value_1":
ContentValues values = new ContentValues(); values.put("column_1", "value_1");
Once received, the content provider will (in your case) pass the
values object to your
SQLiteDatabase (via the
SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values) method). Unlike the
ContentProvider, this method is implemented for you... the
SQLiteDatabase knows how to handle the
values object and will insert the row into the database, returning the row id of the inserted row, or
-1 if the insertion failed.
... and that's how you insert data into your database.