Multi-Table Content Provider

I am new to Android and am implementing a content provider for 5 tables. My questions: Do I have to have a content provider for each table or several tables in one Service Provider? Because the content provider has one method to insert, update, query, delete.

How to include only one content provider in the application? I searched and in most examples, I find only one table application.

Where do I need to use switch conditions to support multiple tables with the same content provider?

please give me some idea.

+4
source share
2 answers

You can use the URI parameter:

List<String> android.net.Uri.getPathSegments() 

If your URI, for example:

 content://com.mypackage.MyContentProvider/MyTable 

MyTable will be in the list returned by getPathSegments(); .

Then you need to specify your table in the URI and in the insert, update, query, delete methods in the provider, construct the query depending on the URI parameter.

To avoid testing in a URI, you can add an Annotation method called getTableName() to your provider, which will return your tableName as a String .

Then extend your provider to the 5 classes Table1Provider , Table2Provider , etc. and implement the method

 Class abstract MyProvider extends ContentProvider{ public abstract String getTableName(); @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { ///... // Set the table queryBuilder.setTables(getTableName()); //... return cursor; } } class Table1Provider extend MyProvider{ public String getTableName(){ return "Table1"; } 

Then create an instance of Table1Provider instead of the abstract provider.

+1
source

Make one supplier. Use the Android provided URIMatcher class to match content URIs across different tables.

Read here: http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI

0
source

All Articles