我应该如何向ContentProvider发送参数?

时间:2015-10-19 10:12:03

标签: android android-contentprovider

我最近创建了我的第一个内容提供商。我在向内容提供商发送参数(例如项目ID)时遇到了一些麻烦。

假设我有以下URIMatcher

private static UriMatcher buildUriMatcher() {
    final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    matcher.addURI(DatabaseContract.CONTENT_AUTHORITY, DatabaseContract.PATH_ITEM + "/#", ITEM_WITH_ID);

    return matcher;
}

我可以使用此加载程序发送给定的id

//...
cursorLoader = new CursorLoader(activity,
    DatabaseContract.ItemEntry.CONTENT_URI.buildUpon().appendPath(itemId+"").build(),
    null,
    DatabaseContract.ItemEntry.COLUMN_ID + "= ?",
    new String[]{itemId},
    null);
//...

或者我可以使用另一个

//...
cursorLoader = new CursorLoader(activity,
    DatabaseContract.ItemEntry.CONTENT_URI.buildUpon().appendPath(itemId+"").build(),
    null,
    null,
    null,
    null);
//...

使用

从内容提供程序中的Uri中检索id
String itemId = uri.getLastPathSegment();

我应该使用上面哪个装载机?为什么?

为什么我问这个问题

http url和内容提供者uri之间没有太多差异。目前,大多数Web框架/库(如angular或vertx)使用路由器从uri检索数据。 为什么android没有?它可以基于UriMatcher轻松完成......

0 个答案:

没有答案