views:

56

answers:

1

The ContentProvider doc says to make ONE entry in the AndroidManifest for your ContentProvider class. If your class supports multiple sub-tables then there must be one CONTENT_URI constant declared for each. How? You can't do that unless you sub-class for each sub-table. Why not just have multiple providers?

Do you implement the sub-table providers as descendants? With multiple sub-tables, there is still only ONE ContentProvider class?

As you can see, I'm confused by the documentation. It reads:

Define a public static final Uri named CONTENT_URI. This is the string that represents the full content: URI that your content provider handles. You must define a unique string for this value. The best solution is to use the fully-qualified class name of the content provider (made lowercase). So, for example, the URI for a TransportationProvider class could be defined as follows:

public static final Uri CONTENT_URI = 
               Uri.parse("content://com.example.codelab.transporationprovider");

If the provider has subtables, also define CONTENT_URI constants for each of the subtables. These URIs should all have the same authority (since that identifies the content provider), and be distinguished only by their paths. For example:

content://com.example.codelab.transporationprovider/train 
content://com.example.codelab.transporationprovider/air/domestic 
content://com.example.codelab.transporationprovider/air/international

So, how many classes do we create to handle train, air/domestic and air/international?

+1  A: 

If your class supports multiple sub-tables then there must be one CONTENT_URI constant declared for each. How? You can't do that unless you sub-class for each sub-table.

Don't name them all CONTENT_URI, then. That name isn't terribly useful for third parties, anyway, since they won't have access to your source code to access that static data member. The documentation confused me too, and I even kinda parrot their instructions in my one book, but I am moving away from that and will be revamping my materials to match.

A better place to look is their own content providers (ContactsContract, CallLog, etc.).

Do you implement the sub-table providers as descendants? With multiple sub-tables, there is still only ONE ContentProvider class?

Have as many as you want. You can do it with a single class, or with inner classes (see ContactsContract), or whatever.

CommonsWare
Ahh, got it! I was very surprised to see the doc talking about CONTENT_URI as if that was somehow recognized by reflection or something, but I guess even that isn't possible. So how do users discover the actual values for CONTENT_URI? From external doc?
Mark Wigzell
Yup. You just have to tell them what value to use. The Android OS has an advantage: everyone has access to it. Third-party content providers aren't so lucky. It also means that you better stick with whatever names you choose, or use some versioning (e.g., original name for v1 of your schema, different name for v2).
CommonsWare