0

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.

Top Serious
  • 149
  • 1
  • 1
  • 7
  • 2
    "App crashes" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jul 25 '17 at 13:23
  • @CommonsWare ,It says Insertion is not supported for the URI. The URI is 300% correct. So what? – Top Serious Jul 25 '17 at 13:28
  • have you run once and change your db schema in that case you need to reinstall your app. – Deep Naik Jul 25 '17 at 13:28
  • @DeepNaik The data actually go into the database but it just crashes. When I reopen the app, the data is there. – Top Serious Jul 25 '17 at 13:32

1 Answers1

4

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.

Valentun
  • 1,661
  • 12
  • 23