zoukankan      html  css  js  c++  java
  • UriMatcher 和 ContentUris 的用法

    一.UriMatcher

    UriMatcher 用来匹配Uri;使用addURI向UriMatcher中注册Uri。然后使用UriMatcher判断一个uri是否存在

    1.创建:

    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  

    2.注册Uri

    private int student=1;

    private int studentId=2;

    //向UriMatcher中注册Uri

    matcher.addURI("com.example.shujuku", "students", student);  

    matcher.addURI("com.example.shujuku", "students/#", studentId);  

    第三个参数:匹配返回码

    3.把现有的uri与注册的uri进行匹配

    Uri uri = Uri.parse("content://" + "com.example.shujuku" + "/strdents");  
    //match()方法匹配后会返回一个“匹配码”,这个匹配码
    int match = matcher.match(uri); switch (match) { case student: return "vnd.android.cursor.dir/strdents"; case studentId: return "vnd.android.cursor.item/strdents"; default: return null; }

    UriMatcher.NO_MATCH 没有匹配的Uri返回码(在实例化UriMatcher 时候传入)

    # 号为通配符

    * 号为任意字符

    二。ContentUris

    ContentUris主要获取Uri后面的id。

    1.获取Uri路径中的Id

    Uri uri = Uri.parse("content://com.example.shujuku/students/18")

    long personid = ContentUris.parseId(uri);

    2.为Uri路径添加Id

    Uri uri = Uri.parse("content://com.example.shujuku/students")  

    Uri uri_id= ContentUris.withAppendedId(uri, 18);

    主要用到了parseId()和withAppendedId()两个方法。一个是用来获取Uri路径中的Id,一个是为Uri添加Id。

  • 相关阅读:
    python中的单向链表实现
    Django中的Form表单验证
    顺序表的原理与python中的list类型
    HttpServletRequest get
    maven压缩js css
    left join inner join 区别
    Spark运行模式:cluster与client
    java 变量 final 小结
    eclipse svn 删除不了项目,合并不了问题
    hadoop HDFS常用文件操作命令 (转)
  • 原文地址:https://www.cnblogs.com/ywtk/p/3804531.html
Copyright © 2011-2022 走看看