Skip to content

Instantly share code, notes, and snippets.

@parahall
Last active March 1, 2017 19:10
Show Gist options
  • Save parahall/8696907731265117f539f083ed77da07 to your computer and use it in GitHub Desktop.
Save parahall/8696907731265117f539f083ed77da07 to your computer and use it in GitHub Desktop.
KolGeneProvider.java
public class KolGeneProvider extends ContentProvider {
//...
@Nullable @Override public Uri insert(@NonNull Uri uri, ContentValues values) {
//open DB for write
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
//match URI to action.
final int match = sUriMatcher.match(uri);
Uri returnUri;
switch (match) {
//case of creating order.
case ORDER:
long _id = db.insertWithOnConflict(KolGeneContract.OrderEntry.TABLE_NAME, null, values,
SQLiteDatabase.CONFLICT_REPLACE);
if (_id > 0) {
returnUri = KolGeneContract.OrderEntry.buildOrderUriWithId(_id);
} else {
throw new android.database.SQLException(
"Failed to insert row into " + uri + " id=" + _id);
}
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
//notify observables about the change
getContext().getContentResolver().notifyChange(uri, null);
return returnUri;
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment