What is the right way to get data from a content provider if you have a uri string?

I was wondering if it is possible to get the data that is on the line of the content provider directly in the cursor (or another container) in the case when I already know the uri line (in other words, I already have ContentURIs with the added line number).

Both insert and Content Observer OnChange (on android api 16) returns me an inserted / modified URI, and I would like to efficiently get data from this string (without many requests or instructions). However, the only way I found is to parse the identifier from the URI, build a query with a suggestion to highlight this id, and then get the cursor like:

long row = ContentUris.parseId(uri); String mSelectionClause = ContentProvider.Table._ID + " = ?"; String[] mSelectionArgs = {Long.toString(row)}; Uri other_uri = Uri.parse(ContentProvider.AUTHORITY_STRING + ContentProvider.UriPathIndex.Table); Cursor cursor = cr.query(other_uri ,null,mSelectionClause,mSelectionArgs,null); 

Is there any other way to do this? I was wondering if uri string can be used directly

+4
source share
1 answer

Quoting from http://developer.android.com/guide/topics/providers/content-provider-basics.html#ContentURIs

Many providers allow you to access a single row in a table by adding an ID to the end of the URI. For example, to retrieve a string whose _ID is 4 from a user dictionary, you can use this content URI:

 Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4); 

EDIT (from comments): It works for all standardized content providers (at least provided by Google). It will work for all content providers that follow design guidelines for content providers backed by row-based data storage.

+5
source

Source: https://habr.com/ru/post/1442303/


All Articles