How to get id from insert ()

I have a little problem getting the id from the added row. insertWithConflictinside my ContentProviderreturns the identifier of the line just added, but I don’t know how to get this from mine ContentProviderto mine IntentService, responsible for including the insert.

CODE

Paste in IntentService

Uri uri = Uri.parse(MicroDatabaseProvider.CONTENT_URI + "/" +  MicroDatabaseProvider.ORGANIZER_SEARCH_IN);
getContentResolver().insert(uri, cv);

Paste 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);
+4
source share
1 answer

Your function insertin ContentProvidershould return a value Urias ContentProviderbelow:

public Uri insert(Uri uri, ContentValues values) {
//...
return Uri.parse(base_uri+ "/" + id); //where base uri is the base uri from your table
}

If so, as I showed you, you can get the identifier from this URI.

Use the following code in your IntentService

Uri result = getContentResolver().insert(uri, cv); 

long id = Long.parseLong(uri.getLastPathSegment());
+9
source

All Articles