Frage

I am trying to understand Android REST-style ContentProvider URIs. Assume that I have a database of messages, each of which has an "_id" and "recipient" field (among others).

I understand how to get all columns of all messages:

// Get all messages.
URI uri = Uri.parse("content://" + AUTHORITY + "/messages");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

I understand how to get the message with an _id of 1:

// Get the message with _id 1.
URI uri = Uri.parse("content://" + AUTHORITY + "/messages/1");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

What if I want to get the messages with a recipient value of foo@bar.com?

// Get the messages with recipient "foo@bar.com"
URI uri = Uri.parse("content://" + AUTHORITY + ??);  // What goes here?
Cursor cursor = getContentResolver().query(uri, null, 
    "recipient = ?", "foo@bar.com", null);

I'd be happy to pass the recipient value in the URI if that's better style:

// Get the messages with recipient "foo@bar.com"/
URI uri = Uri.parse("content://" + AUTHORITY + ?? + "foo@bar.com");  // What goes here?
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

For this last use case, what is the preferred URI format?

Update: I am the author of both the ContentProvider and the client, so I can use whatever format I want. I would still like to know what's considered the best practice

War es hilfreich?

Lösung

In this last case, what should go after the AUTHORITY in the URI?

Whatever you want. If you are writing the ContentProvider, it is up to you to define what your Uri syntax means, for the path segments to the right of the authority.

In your case, you are passing the value (foo@bar.com) via query parameters. Hence, your ?? can be:

  • nothing at all
  • some path segment that helps you identify what sort of table you are querying, like messages
  • asrasiojdflkasdjkl90238490qa89dsfjlas or some other random string
  • etc.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top