Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 544
tyczj Avatar asked Jul 14 '12 23:07

tyczj


3 Answers

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

like image 135
Y2i Avatar answered Oct 23 '22 08:10

Y2i


long id = Long.valueOf(uri.getLastPathSegment());
like image 31
Alex Lockwood Avatar answered Oct 23 '22 08:10

Alex Lockwood


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

Read Documentation in here

like image 1
hosseinAmini Avatar answered Oct 23 '22 08:10

hosseinAmini