4

I am new in Android and am implementing a content provider for 5 tables. My Questions Are: Should I have a Content Provider for each table or multiple tables in a single Content provider? Since the Content Provider has a single insert, update, query, delete methods.

How can I include only one Content Provider in my application? I have searched and in Most of the examples, I only find a single table apps.

Where do I have to use switch conditions to support multiple tables with the same Content Provider?

please give me some idea.

Trupti
  • 284
  • 5
  • 7
  • single insert, update, query, delete these methods ask for table name right?. what is the issue then – AAnkit Apr 04 '13 at 09:51
  • 1
    i want to insert or delete or update in more than one table but content provider have only have one insert,one delete ... method so how can i do it – Trupti Apr 04 '13 at 09:53
  • Why dont you call these methods multiple times for each table? – AAnkit Apr 04 '13 at 11:47
  • how to call these method multiple times, please give some example, actually i am new to android so donot have more idea about it , as this is my ist application. @AndroidEnthusiast – Trupti Apr 06 '13 at 04:58
  • same way u are calling single time, Use for loop instead, change method argument. – AAnkit Apr 06 '13 at 05:01
  • whether i should call these methods by using loop or i have to use UriMatcher and for each method i have switch case where each table has 2 case: switch (uriType) { case TUTORIAL_ID: ,case TUTORIALS} @ AndroidEnthusiast – Trupti Apr 06 '13 at 07:49
  • possible duplicate of [Own ContentProvider with SQLite and multiple tables](http://stackoverflow.com/questions/13572352/own-contentprovider-with-sqlite-and-multiple-tables) – Avinash R Feb 13 '14 at 11:32

2 Answers2

1

You can use the URI parameter:

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

If your URI is, for example:

content://com.mypackage.MyContentProvider/MyTable

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

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

To avoid testing on URI you can add to you provider an Abstract method called getTableName() witch will return your tableName as String.

Then extend your provider to 5 classes Table1Provider, Table2Provider etc. and implement 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 instantiate the Table1Provider instead of the abstract provider.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Simo
  • 1,200
  • 1
  • 16
  • 26
  • means i have to write all these method using switch case. like TODO and TODO_ID for 5 table have 10 cases ??? – Trupti Apr 04 '13 at 10:59
  • what's the problem with using switch statements? – Android Noob Jun 30 '13 at 18:35
  • 1
    using switch statements for this example isn't a problem but if you have a more complex example (witch is the case in general) you will have a lot of "switching" witch isn't "elegant" in an object language – Simo Jul 01 '13 at 08:09
0

Make one provider. Use the URIMatcher class provided by Android to match content URIs with different tables.

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

Nishad
  • 1,311
  • 1
  • 9
  • 15