4

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); 

1 Answer 1

11

Your insert function in your ContentProvidershould 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()); 
Sign up to request clarification or add additional context in comments.

5 Comments

I did that and the URI was microdatabase/0, which I know is false because at the same time I logged my idLong and it was 34.
Show then your return statement in your ContentProvider
OH MY I AM SO DENSE ....... return Uri.parse(BASE_PATH + "/" + id); it should be idLong
Thanks for help, will mark as solved as soon as I can.
getLastPathSegment() returning String

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.