2013-11-23 36 views

回答

2

随着ContentProviders,您正在使用的contentURL这将是这个样子

content://<authority>/<data_type>/<id> 

权威是内容提供商名称查询数据,例如联系人或定制的联系人将是com.xxxxx.yyy。

DATA_TYPEID是指定你所需要的数据的提供,如果需要,该键的特定值。

因此,如果您要构建自定义内容提供者,则需要解析在查询函数中作为参数获取的内容uri,并决定需要返回哪些数据作为光标。 UriMatcher类是这种情况下非常好的选择。下面是一个例子

static final String URL = "content://com.mycompany.myapp/students"; 
static final Uri CONTENT_URI = Uri.parse(URL); 

static final UriMatcher uriMatcher; 
static{ 
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
    uriMatcher.addURI("com.mycompany.myapp", "students", 1); 
    uriMatcher.addURI("com.mycompany.myapp", "students/#", 2); 
} 

然后在您的查询的功能,你就会有这样的事情:

switch (uriMatcher.match(uri)) { 
    case 1: 
    // we are querying for all students 
    // return a cursor all students e.g. "SELECT * FROM students" 
    break; 
    case 2: 
    // we are querying for all students 
    // return a cursor for the student matching the given id (the last portion of uri) 
    // e.g. "SELECT * FROM students WHERE _id = n" 
    break; 
    default: 
    throw new IllegalArgumentException("Unknown URI " + uri); 
    } 

我希望这回答了你的问题,并指导您正确的轨道。

你可以看到关于如何使用它们,这里 http://www.tutorialspoint.com/android/android_content_providers.htm

完整的例子好文章