@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Uisng SQLiteQueryBuilder instead of query() method SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); // check if the caller has requested a column which does not exists checkColumns(projection); // Set the table queryBuilder.setTables(RescueTable.RESCUE_TABLE); int uriType = sURIMatcher.match(uri); switch (uriType) { case URI_MATCHER: break; case URI_MATCHER_ID: // adding the ID to the original query queryBuilder.appendWhere(RescueTable.COLUMN_ID + "=" + uri.getLastPathSegment()); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } SQLiteDatabase db = database.getWritableDatabase(); Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder); // make sure that potential listeners are getting notified cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int uriType = sURIMatcher.match(uri); SQLiteDatabase sqlDB = database.getWritableDatabase(); int rowsUpdated = 0; switch (uriType) { case URI_MATCHER: rowsUpdated = sqlDB.update(RescueTable.RESCUE_TABLE, values, selection, selectionArgs); break; case URI_MATCHER_ID: String id = uri.getLastPathSegment(); if (TextUtils.isEmpty(selection)) { rowsUpdated = sqlDB.update( RescueTable.RESCUE_TABLE, values, RescueTable.COLUMN_ID + "=" + id, null); } else { rowsUpdated = sqlDB.update( RescueTable.RESCUE_TABLE, values, RescueTable.COLUMN_ID + "=" + id + " and " + selection, selectionArgs); } break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return rowsUpdated; }
@Override public Uri insert(Uri uri, ContentValues values) { int uriType = sURIMatcher.match(uri); SQLiteDatabase sqlDB = database.getWritableDatabase(); int rowsDeleted = 0; long id = 0; switch (uriType) { case URI_MATCHER: // Zero count means empty table. id = sqlDB.insert(RescueTable.RESCUE_TABLE, null, values); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return Uri.parse(BASE_PATH_RESCUE + "/" + id); }