9

There is distinct recomendation in documentation for defining all uris while implementing ContentProvider. But I'm confused with URI matcher part: for example, I've got package org.company.example, table named 'items', then I define

 public static final Uri CONTENT_URI = 
  Uri.parse("content://org.company.example.sampleprovider/items");

And what authority part should I use for matching URIs in static init:

 private static final UriMatcher uriMatcher;

  static {
   uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
   uriMatcher.addURI("what goes here?", "items", ITEM);
   uriMatcher.addURI("what goes here?", "items/#", ITEM_ID);
  }
louielouie
  • 14,881
  • 3
  • 26
  • 31

1 Answers1

14
public static final String PROVIDER_NAME = "org.company.example.sampleprovider";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME);
private static final UriMatcher uriMatcher;
static {
   uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
   uriMatcher.addURI(PROVIDER_NAME, "items", ITEM);
   uriMatcher.addURI(PROVIDER_NAME, "items/#", ITEM_ID);
}
Pim Reijersen
  • 1,123
  • 9
  • 33
  • shouldn't my CONTENT_URI contain path to table? –  Jun 03 '11 at 11:39
  • 1
    You do not have to, but it is possible. Its for you to decide. My content providers are already named like "BooksProvider" so I do not need to add the extra "/books" to make clear to the user what the provider does. – Pim Reijersen Jun 03 '11 at 11:43