This the code:
For insert
of ContentProvider
:
public Uri insert(Uri uri, ContentValues contentValues) {
final int match = sUriMatcher.match(uri);
switch (match) {
case DOG:
insertDog(uri, contentValues);
default:
throw new IllegalArgumentException("Insertion is not supported for " + uri);
}
}
private Uri insertDog(Uri uri, ContentValues contentValues) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
long id = db.insert(DogEntry.TABLE_NAME, null, contentValues);
if (id == -1) {
Log.e(LOG_TAG, "Failed to insert row for " + uri);
return null;
}
return ContentUris.withAppendedId(uri, id);
}
This is where I insert data:
ContentValues values = new ContentValues();
values.put(DogEntry.NAME, "Doggy");
values.put(DogEntry.BREED, "Dingo");
values.put(DogEntry.GENDER, DogEntry.GENDER_MALE);
values.put(DogEntry.WEIGHT, 15);
Uri newUri = getContentResolver().insert(DogEntry.CONTENT_URI,values);
Note: All constants are defined in a contract class.
You forget return:
...
case DOG:
return insertDog(uri, contentValues);
Explanations: switch goes through all cases and default at the end. We should call break or return in case statements.