Ellen Spertus
Ellen Spertus

Reputation: 6805

Android ContentProvider URI format for select queries not on _id

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 [email protected]?

// Get the messages with recipient "[email protected]"
URI uri = Uri.parse("content://" + AUTHORITY + ??);  // What goes here?
Cursor cursor = getContentResolver().query(uri, null, 
    "recipient = ?", "[email protected]", null);

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

// Get the messages with recipient "[email protected]"/
URI uri = Uri.parse("content://" + AUTHORITY + ?? + "[email protected]");  // 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

Upvotes: 1

Views: 840

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

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 ([email protected]) 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.

Upvotes: 2

Related Questions