在 Android 应用程序中,每个表应该有一个内容提供程序还是整个应用程序只有一个内容提供程序?
我拥有多年的 Microsoft .NET 开发(主要是 C#)经验,并且一直致力于加快 Android 和 Java 的开发速度。到目前为止,我已经构建了一个带有几个屏幕和一个工作内容提供程序的小型应用程序。
我见过的用于开发内容提供程序的所有示例通常都使用单个表,因此我的印象是这是惯例。
我为其他表构建了更多内容提供程序,并在尝试测试它们时遇到了“未知 URI”IllegalArgumentException。我的内容提供商之一抛出了异常,但不是我打算调用的内容提供商。
看来我的应用程序正在使用 AndroidManifest.xml 文件中的第一个内容提供程序,这让我想知道是否应该为整个应用程序只使用一个内容提供程序。
是否有在 Android 应用程序中使用多个表的最佳实践和/或示例?我应该为每个表配备一个内容提供商还是为整个应用程序配备一个内容提供商?如果是前者,我如何将 URI 解析为正确的提供者?如果是后者,我如何防止我的内容提供程序代码被 switch 语句污染?
I have years of experience with Microsoft .NET development (primarily C#) and have been working to come up to speed on Android and Java. So far, I've built a small application with a couple screens and a working content provider.
All of the examples I've seen for developing content providers typically work with a single table, so I got the impression that this was the convention.
I built a couple more content providers for other tables and ran into the "Unknown URI" IllegalArgumentException when I tried to test them. The exception is being thrown by one of my content providers, but not the one I was intending to call.
It appears that my application is using the first content provider in the AndroidManifest.xml file, which now has me wondering if I should only have a single content provider for the entire application.
Are there any best practices and/or examples for working with multiple tables in an Android application? Should I have one content provider per table or only one for the entire application? If the former, how do I resolve URIs to the proper provider? If the latter, how do I keep my content provider code from being polluted with switch statements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不同意 CommonsWare 的观点。
如果你想避免 IllegalStateExceptions 和其他问题,你需要使用 Cursorloader。他们为您处理多项事情并确保光标光滑。
因此您需要内容提供商。
最初的问题尚未得到解答。我不知道数字或内容提供商的最佳做法是什么?表。但在 .query 方法中,您检查 URI id。您可以测试 uri id 是否具有特定值并以这种方式构建查询。
Well i have to disagree with CommonsWare.
If you want to avoid IllegalStateExceptions and other problems you need to use Cursorloader. They handle multiple things for you and make sure cursors are slick.
Therefore you need Content Providers.
The initial question is not answered yet thought. i don't know whats best practise for the number or content providers & tables. but within the .query method you check the URI id. you can test if the uri id has a specific value and build your query that way.
99% 的 Android 应用程序不需要内容提供程序。
内容提供者有一个主要用途:进程间通信。如果您:
那么,只有这样,您才实现内容提供程序。
否则,您所做的只是增加应用程序的开销并使代码更难以维护。
因此,如果您的应用程序是 99% 不需要内容提供程序的应用程序之一,只需删除现有的应用程序并通过其他方式(例如,
SQLiteDatabase
)访问您的内容即可。99% of Android applications do not need content providers.
Content providers have one primary use: inter-process communication. If you are:
then, and only then, do you implement a content provider.
Otherwise, all you are doing is adding overhead to your application and making your code more difficult to maintain.
So, if your application is one of the 99% that does not need content providers, just get rid of the one you have and access your content by some other means (e.g.,
SQLiteDatabase
).