Content Provider 中 UriMatcher的用法
来源:互联网 发布:udp socket编程 编辑:程序博客网 时间:2024/05/17 07:04
ContentProvider是Android四大组件之一,网上也有不少关于它的文章,基本用法都可以查到,但关于UriMatcher在其中的作用,文章中都有例子,但我觉得还没有说清楚。
先说为什么用UriMatcher。
ContentProvider向外界提供了一个标准的,也是唯一的用于查询的接口:
public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);
其中uri用于指定哪一个数据源,当一个数据源含有多个内容(比如多个表),就需要用不同的Uri进行区分,例如:
public static final Uri CONTENT_URI_A = Uri.parse("content://" + AUTHORITY + "/" + TABLE_A);public static final Uri CONTENT_URI_B = Uri.parse("content://" + AUTHORITY + "/" + TABLE_B);
这时候使用UriMatcher就可以帮助我们方便的过滤到TableA还是TableB, 然后进行下一步查询, 如果不用UriMatcher也可以,我们就需要手动过滤字符串,用起来有点麻烦,可维护性也不好。
再说怎么用UriMatcher, 定义如下:
// Set up our URL matchers to help us determine what an // incoming URI parameter is. private static final UriMatcher URI_MATCHER; static { URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); URI_MATCHER.addURI(AUTHORITY, TABLE_A, TABLE_A_MSG); URI_MATCHER.addURI(AUTHORITY, TABLE_B, TABLE_B_MSG); }
在查询中使用UriMatcher:
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { String table = null; switch (URI_MATCHER.match(uri)) { case ALL_MESSAGES: break; case OXFORD_MSG: table = TABLE_A; break; case CHENYU_MSG: table = TABLE_B; break; default: break; } Cursor resultCursor = mDB.query(table, projection, selection, selectionArgs, null, null, sortOrder); return resultCursor; }
总之,UriMatcher本质上是一个文本过滤器,用在contentProvider中帮助我们过滤,分辨出查询者想要查询哪个数据表。
- Content Provider 中 UriMatcher的用法
- 【Content Provider】UriMatcher类
- 【Content Provider】Uri、UriMatcher、ContentUris详解
- 【Content Provider】Android的CursorLoader用法小结
- UriMatcher的用法
- Content Provider 用法
- Content Provider中主键的问题
- content provider 的使用
- Android的Content Provider
- Content Provider的加载
- Content Provider的权限
- content provider的使用!
- 简单的content provider
- Content Provider的加载
- UriMatcher用法
- Content provider 中getPathSegments用途
- Content provider 中getPathSegments用途
- Content provider 中getPathSegments用途
- GridView自动生成ID
- Ajax 处理 Json 数据
- GridView实现数字页码分页
- Office2010 与EndNoteX3冲突
- POJ 1700 Crossing River 贪心策略
- Content Provider 中 UriMatcher的用法
- Twitter strom 集群搭建
- HTML5 俄罗斯方块 V1.0
- 《3 idiots 》主题曲Behti Hawa Sa Tha Woh的歌词和mp3下载链接
- org.apache.axis2.AxisFault: Transport error: 403 Error: Forbidden
- textContent与innerText的不同(转)
- 第十六周实验报告1
- 第十六周实验报告3
- 第十七周实验报告1