具有多个子表的 ContentProvider 的类结构
ContentProvider 文档说要在 AndroidManifest 中为您的 ContentProvider 类创建一个条目。如果您的类支持多个子表,则必须为每个子表声明一个 CONTENT_URI 常量。如何?除非您对每个子表进行子类化,否则您不能这样做。为什么不直接拥有多个提供商呢?
您是否将子表提供者实现为后代?有了多个子表,仍然只有一个ContentProvider类?
正如你所看到的,我对文档感到困惑。上面写着:
定义一个名为的公共静态最终 Uri CONTENT_URI。这是字符串 代表完整内容:URI 您的内容提供商处理。你 必须为此定义一个唯一的字符串 价值。最好的解决方案是使用 的完全限定类名 内容提供者(小写)。所以, 例如,一个 URI TransportProvider 类可以是 定义如下:
public static final Uri CONTENT_URI =
Uri.parse("content://com.example.codelab.transporationprovider");
如果提供者有子表,还为每个子表定义 CONTENT_URI 常量。这些 URI 应该都具有相同的权限(因为它标识内容提供者),并且仅通过它们的路径来区分。例如:
content://com.example.codelab.transporationprovider/train
content://com.example.codelab.transporationprovider/air/domestic
content://com.example.codelab.transporationprovider/air/international
那么,我们创建多少个类来处理火车、航空/国内和航空/国际?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,不要将它们全部命名为
CONTENT_URI
。无论如何,该名称对于第三方来说并不是很有用,因为他们无法访问您的源代码来访问该静态数据成员。这些文档也让我感到困惑,我什至在我的一本书中模仿了他们的说明,但我正在放弃这种做法,并将修改我的材料以匹配。更好的地方是他们自己的内容提供程序(
ContactsContract
、CallLog
等)。你想要多少就拥有多少。您可以使用单个类或内部类(请参阅
ContactsContract
)或其他任何方式来完成此操作。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.).Have as many as you want. You can do it with a single class, or with inner classes (see
ContactsContract
), or whatever.