Skip to content Skip to sidebar Skip to footer

Get The Id Of A New Record Inserted Into A Database From The Returned Uri

When you insert a new item into a database like this Uri uri = getContentResolver().insert(Tournaments.CONTENT_URI, values); is there any way to get the id from the uri that is

Solution 1:

ContentUris.parseId() converts the last path segment to a long.

Solution 2:

longid = Long.valueOf(uri.getLastPathSegment());

Solution 3:

Use ContentUris.parseId(uri) This statement will Converts the last path segment to a long.

Read Documentation in here

Solution 4:

long rowId = Long.valueOf(uri.getLastPathSegment());

The above is correct. But what I did is, I passed uri like Events.CONTENT_URI.getLastPathSegment() which doesn't give the id. Because we have to pass the URI which is returned by insert() like below. May be helpful to someone like me!

Uriuri= getContentResolver().insert(Events.CONTENT_URI, values); 
longrowId= Long.valueOf(uri.getLastPathSegment());

Post a Comment for "Get The Id Of A New Record Inserted Into A Database From The Returned Uri"