Implementing an Insert Method

The insert method in a content provider is responsible for inserting a record into the underlying database and then returning a URI that points to the newly created record.

Like the other methods, insert uses UriMatcher to identify the URI type. The code first checks whether the URI indicates the proper collection-type URI. If not, the code throws an exception (see Listing 3-28).

The code then validates the optional and mandatory column parameters. The code can substitute default values for some columns if they are missing.

Next, the code uses a SOLiteDatabase object to insert the new record and returns the newly inserted ID. In the end, the code constructs the new URI using the returned ID from the database.

Listing 3-28. The insertO Method Implementation

^Override

0 0

Post a comment