2012-07-14 8 views

risposta

63

ContentUris.parseId() converte l'ultimo segmento del percorso in un lungo.

+5

ContentUris.parseId() è "ufficiale" risposta di Google su come recuperare gli ID dei record, vedere alla fine del [dati Inserimento] (http://developer.android.com/guide/topics/providers/ content-provider-basics.html # Inserting) section. È anche possibile utilizzare uri.getLastPathSegment() per ottenere un valore stringa, ma è necessario utilizzare Long.valueOf (stringId) anziché Integer.valueOf (stringId) perché gli ID del database hanno valori lunghi. – Y2i

11
long id = Long.valueOf(uri.getLastPathSegment()); 
+0

fa uri.getPathSegments(). Get (1) dà anche gli stessi ID di –

+0

sono 'lunghi' – weston

0

Usa ContentUris.parseId (URI) presente informativa Converte l'ultimo segmento di percorso per un lungo.

leggere la documentazione in here

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

Quanto sopra è corretto. Ma quello che ho fatto è che ho passato uri come Events.CONTENT_URI.getLastPathSegment() che non fornisce l'id. Perché dobbiamo passare l'URI che viene restituito da insert() come sotto. Potrebbe essere utile a qualcuno come me!

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