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

前端 未结 4 1629
不思量自难忘°
不思量自难忘° 2020-12-24 11:33

When you insert a new item into a database like this

Uri uri = getContentResolver().insert(Tournaments.CONTENT_URI, values); 

is there any

相关标签:
4条回答
  • 2020-12-24 12:16
    long id = Long.valueOf(uri.getLastPathSegment());
    
    0 讨论(0)
  • 2020-12-24 12:26
    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());
    
    0 讨论(0)
  • 2020-12-24 12:36

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

    Read Documentation in here

    0 讨论(0)
  • 2020-12-24 12:38

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

    0 讨论(0)
提交回复
热议问题