开发者

In an Android application, should I have one content provider per table or only one for the entire application?

开发者 https://www.devze.com 2023-01-01 18:24 出处:网络
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 an

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 开发者_如何学JAVAfor 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?


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% of Android applications do not need content providers.

Content providers have one primary use: inter-process communication. If you are:

  • expecting other developers to access your content providers, or
  • are integrating to Android in places that require a content provider (e.g., search suggestions for the Quick Search Box)

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消