Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 727
Andy Dyer Avatar asked May 29 '10 19:05

Andy Dyer


People also ask

How many content providers can an app have?

You can implement as many as you want, as you can see from the documentation here. To register a content provider, you need to add its corresponding <provider> tag in the Android Manifest. In most cases, however, you won't need multiple content providers. One is usually enough, as it can handle multiple tables.

What is the main usage of content provider in Android?

The role of the content provider in the android system is like a central repository in which data of the applications are stored, and it facilitates other applications to securely access and modifies that data based on the user requirements.

What are the various content providers in Android applications?

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

Why does the content provider need to be declared in the Android manifest?

A content provider is a subclass of ContentProvider that supplies structured access to data managed by the application. All content providers in your application must be defined in a <provider> element in the manifest file; otherwise, the system is unaware of them and doesn't run them.


1 Answers

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.

like image 160
omni Avatar answered Oct 03 '22 10:10

omni