android之uriMathcer详解及使用
2012-09-30 10:21
543 查看
UriMatcher是一个工具类,主要是用于contentProvider中用于匹配URIS。
UriMatcher实际上相当于一棵树,实例化的UriMatcher对象,相当于树的根节点。
UriMatcher的实例化,
UriMatcher matcher=new UriMatcher(UriMatcher.NO_MACTHER);
UriMatcher.NO_MACTHER是一个常量,如果不匹配就返回-1.
addURI是添加一个uri,如果这个URi匹配则返回匹配码,不匹配则返回-1.
从以创建的uri树中去匹配传进来的uri,如果匹配成功,则返回匹配码,否则-1.
下面通过代码去创建一棵树(只有两个节点,文档上的比较多)
public static final int PERSON = 1;//状态码
public static final int NUMBER = 2;
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI("com.example.sqlite", "person", PERSON);
matcher.addURI("com.example.sqlite", "person/#", NUMBER);//#代表任意数字
匹配uri
int code = matcher.match(uri);
SQLiteDatabase data=db.getWritableDatabase();
switch (code) {
case PERSON:
data.delete("person",selection,selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
case NUMBER:
int id=(int) ContentUris.parseId(uri);
selection=(selection==null)?"id="+id:selection+"and id="+id;
data.delete("person", selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
break;
}
UriMatcher实际上相当于一棵树,实例化的UriMatcher对象,相当于树的根节点。
UriMatcher的实例化,
UriMatcher matcher=new UriMatcher(UriMatcher.NO_MACTHER);
UriMatcher.NO_MACTHER是一个常量,如果不匹配就返回-1.
void | addURI(String authority, String path, int code) |
int | match(Uri uri) |
下面通过代码去创建一棵树(只有两个节点,文档上的比较多)
public static final int PERSON = 1;//状态码
public static final int NUMBER = 2;
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI("com.example.sqlite", "person", PERSON);
matcher.addURI("com.example.sqlite", "person/#", NUMBER);//#代表任意数字
匹配uri
int code = matcher.match(uri);
SQLiteDatabase data=db.getWritableDatabase();
switch (code) {
case PERSON:
data.delete("person",selection,selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
case NUMBER:
int id=(int) ContentUris.parseId(uri);
selection=(selection==null)?"id="+id:selection+"and id="+id;
data.delete("person", selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
break;
}
相关文章推荐
- Android中uri使用场景详解
- Android 进程间通信(AIDL)使用详解
- Android中内容观察者的使用---- ContentObserver类详解
- Android ViewPager使用详解
- Android ORM应用开发框架KJFrameForAndroid使用详解
- Android 四大组件之(3)BroadcastReceiver广播事件处理以及与Notification的配合使用详解
- Android Studio Live Templates使用详解,提高敲代码的速度
- 微信小程序―微信跳一跳,Android游戏助手(外挂)使用教程详解
- Android webview使用详解
- Android中AsyncTask使用详解
- Android使用java的Math.Random获取随机色值
- Android 4.4 KitKat NotificationManagerService使用详解与原理分析(一)__使用详解
- android中Service使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中Service 使用详解(LocalService + RemoteService)
- 使用Math.floor与Math.random取随机整数的方法详解
- android SpannableString使用详解
- Android Volley框架使用详解
- Android AIDL使用详解 实现进程间的通信
- android 之 Handler 详解----(二)常见的使用的基本方式