Domanda

I didn't get the concept of uris in content providers in Android. My database consists of three tables. I never actually query a single table but rather need to use joints to get the wanted data. For now my tables are A,B, C.

private static final String AUTHORITY = "com.example.database.contentprovider";

// uri for each table
public static final Uri CONTENT_URI_A = Uri.parse("content://" + AUTHORITY + "/" + A.TABLE_NAME);
public static final Uri CONTENT_URI_B = Uri.parse("content://" + AUTHORITY + "/" + B.TABLE_NAME);
public static final Uri CONTENT_URI_C = Uri.parse("content://" + AUTHORITY + "/" + C.TABLE_NAME);
  1. Why do I need the AUTHORITY Uri?
  2. Do you I need a Uri for each table or for each possible Query? As I said, I don't need to query exclusively one table and therefore don't see a reason why I need CONTENT_URI_A, CONTENT_URI_B and CONTENT_URI_C.
  3. Do I assume right, that I need URIs to identify what kind of query is going to be made?
È stato utile?

Soluzione

A Content URI is simply a unique string uri associated with a particular query - they do not need to be related to your underlying table structure.

The "content://" + AUTHORITY + "/" part of the URI attempts to ensure that the strings are unique (i.e., they don't conflict with other applications) by using an AUTHORITY that is unique to your application.

They have two main purposes:

  • Allowing your query, update, insert, and delete calls to easily distinguish between different queries (of course, you could use the selection, projection, or other parameters to do this, but often times the URI is much easier to quickly parse using a UriMatcher)
  • Allow you to call Cursor.setNotificationUri on a returned Cursor which will automatically re-query the data when ContentResolver.notifyChange(uri, null) is called (i.e., in your insert/update/delete calls). This is particularly useful when using the Loader Framework and a CursorLoader to automatically receive callbacks when the underlying data changes.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top