简体   繁体   English

多个表的内容提供者

[英]Content provider for multiple tables

I am new in Android and am implementing a content provider for 5 tables. 我是Android新手,我正在为5个表实现内容提供程序。 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. 由于Content Provider具有单个insert,update,query,delete方法。

How can I include only one Content Provider in my application? 如何在我的应用程序中只包含一个Content Provider? 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? 我在哪里必须使用切换条件来支持具有相同Content Provider的多个表?

please give me some idea. 请给我一些想法。

You can use the URI parameter: 您可以使用URI参数:

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

If your URI is, for example: 如果您的URI是,例如:

content://com.mypackage.MyContentProvider/MyTable

MyTable will be in the list returned by getPathSegments(); MyTable将位于getPathSegments();返回的列表中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. 然后,您必须在URI中指定您的表,并在提供程序中的insert,update,query,delete方法中根据URI参数构建查询。

To avoid testing on URI you can add to you provider an Abstract method called getTableName() witch will return your tableName as String . 为了避免在URI上进行测试,您可以向您提供一个名为getTableName()Abstract方法,它会将您的tableName作为String返回。

Then extend your provider to 5 classes Table1Provider , Table2Provider etc. and implement method 然后将您的提供程序扩展到5个类Table1ProviderTable2Provider等并实现方法

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. 然后实例化Table1Provider而不是抽象提供程序。

Make one provider. 建立一个提供商。 Use the URIMatcher class provided by Android to match content URIs with different tables. 使用Android提供的URIMatcher类将内容URI与不同的表进行匹配。

Read here: http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI 请阅读此处: http//developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM