@Override public int delete(Uri uri, String selection, String[] selectionArgs) { final SQLiteDatabase db = flickrDbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); int rowsDeleted; // this makes delete all rows return the number of rows deleted if (selection == null) { selection = "1"; } switch (match) { case PICTURE: rowsDeleted = db.delete(FlickrContract.PictureEntry.TABLE_NAME, selection, selectionArgs); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } // Because a null deletes all rows if (rowsDeleted != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsDeleted; }
@Nullable @Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor; // Here's the switch statement that, given a URI, will determine what kind of request it is, // and query the database accordingly. final int match = uriMatcher.match(uri); if (match == PICTURE) { cursor = flickrDbHelper .getReadableDatabase() .query( FlickrContract.PictureEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); } else if (match == PICTURE_WITH_ID) { cursor = flickrDbHelper .getReadableDatabase() .query( FlickrContract.PictureEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); } else { throw new UnsupportedOperationException("Unknown uri: " + uri); } /** * set notificationUri for the cursor to the one that was passed into the function The cursor * registers the content observer to watch for changes to that uri and any of its descendants to * notify changes on update or insert. */ cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final SQLiteDatabase db = flickrDbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); int rowsUpdated; switch (match) { case PICTURE: rowsUpdated = db.update(FlickrContract.PictureEntry.TABLE_NAME, values, selection, selectionArgs); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (rowsUpdated != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsUpdated; }
@Nullable @Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = flickrDbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); Uri returnUri; switch (match) { case PICTURE: // insert returns the row id of the inserted row, -1 if there's an error long _id = db.insert(FlickrContract.PictureEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = FlickrContract.PictureEntry.buildPictureUri(_id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return returnUri; }