Get the ID of a new record inserted into a database from the returned Uri

Viewed 15595

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 returned so I don't have to run a query to get the id since its already in the returned uri?

4 Answers
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!

Uri uri = getContentResolver().insert(Events.CONTENT_URI, values); 
long rowId = Long.valueOf(uri.getLastPathSegment());
Related