I've got a little problem with retrieving ID from added row.
insertWithConflict
inside my ContentProvider
returns the ID of freshly added row but I don't know a way to get it from my ContentProvider
to my IntentService
responsible for firing insert.
CODE
Insert in IntentService
Uri uri = Uri.parse(MicroDatabaseProvider.CONTENT_URI + "/" + MicroDatabaseProvider.ORGANIZER_SEARCH_IN);
getContentResolver().insert(uri, cv);
Insert in ContetnProvider
long idLong = sqlDB.insertWithOnConflict(DbHelperMicro.ORGANIZER_SEARCH_TABLE,
null,
values,
SQLiteDatabase.CONFLICT_IGNORE);
getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/" + ORGANIZER_SEARCH_QUE), null);
Your insert
function in your ContentProvider
should return an Uri
value like the following:
public Uri insert(Uri uri, ContentValues values) {
//...
return Uri.parse(base_uri+ "/" + id); //where base uri is the base uri from your table
}
If it is like I have shown you, you can retreive the id from that URI.
Use the following code in your IntentService
Uri result = getContentResolver().insert(uri, cv);
long id = Long.parseLong(uri.getLastPathSegment());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With