Android Tutorial

(avery) #1

By : Ketan Bhimani


352 

new UriMatcher(UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, “points”, TRACKPOINTS);
sURIMatcher.addURI(AUTHORITY, “points/#”, TRACKPOINT_ID);
}


First, arbitrary numeric values are defined to identify each different
pattern. Next, a static UriMatcher instance is created for use. The
code parameter that the constructor wants is merely the value to
return when there is no match. A value for this is provided for use
within the UriMatcher class itself.

Next, the URI values are added to the matcher with their
corresponding identifiers. The URIs are broken up in to the
authority portion, defined in AUTHORITY, and the path portion,
which is passed in as a literal string. The path can contain patterns,
such as the “#” symbol to indicate a number. The “*” symbol is
used as a wildcard to match anything.

Implementing the insert() Method

Theinsert() method is used for adding data to the content provider.
Here is a sample implementation of the insert() method:

public Uri insert(Uri uri, ContentValues values) {
int match = sURIMatcher.match(uri);
if (match != TRACKPOINTS) {
throw new IllegalArgumentException(
“Unknown or Invalid URI “ + uri);
}
SQLiteDatabase sqlDB = mDB.getWritableDatabase();
long newID = sqlDB.
insert(TrackPointDatabase.TRACKPOINTS_TABLE, null, values);
if (newID > 0){
Uri newUri = ContentUris.withAppendedId(uri, newID);
getContext()
.getContentResolver().notifyChange(newUri, null);
return newUri;
}
throw new SQLException(“Failed to insert row into “ + uri);
}

Free download pdf