zoukankan      html  css  js  c++  java
  • ContentProvider的创建和使用

    (1)首先创建内容提供者,实现暴露数据库程序的功能 定义一个类继承android.content包下的ContentProvider类,ContentProvider是一个抽象类,使用该类时重写            onCreate() getType() query() insert() delete() update()抽 象方法。

            

    public class PersonDBProvider extends ContentProvider {
        public boolean onCreate() {
            return false;
        }
        public String getType(Uri uri) {
            return null;
        }
        public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
            return null;
        }
        public Uri insert(Uri uri, ContentValues contentValues) {
            return null;
        }
    
        @Override
        public int delete(Uri uri, String s, String[] strings) {
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
            return 0;
        }
    )

    (2)在清单文件里注册内容提供者 (注意:第二个引号中的内容“包名.自定义名(有一定含义的名字)”,第二个引号中的内容“包名.类名”)

     <provider
                android:authorities="com.example.hanshu.first.contentResolver"
                android:name="com.example.hanshu.first.PersonDBProvider">
     </provider>

    (3)定义匹配器和添加匹配规则

           1. 在PersonDBProvider类中定义一个uri的配置器,用于匹配uri,如果路径不满足条件,返回-1;

           2. 添加匹配规则

               具体代码如下:

              

    public class PersonDBProvider extends ContentProvider {
        private static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
        private static final int INSERT=1;
        private static final int QUERY=2;
        private static final int DELETE=3;
        private static final int UPDATE=4;
        static {
            matcher.addURI("com.example.hanshu.first.contentResolver","insert",INSERT);
            matcher.addURI("com.example.hanshu.first.contentResolver","query",QUERY);
            matcher.addURI("com.example.hanshu.first.contentResolver","delete",DELETE);
            matcher.addURI("com.example.hanshu.first.contentResolver","update",UPDATE);
        }
        public boolean onCreate() {
            return false;
        }
        public String getType(Uri uri) {
            return null;
        }
        public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
            return null;
        }
        public Uri insert(Uri uri, ContentValues contentValues) {
            return null;
        }
    
        @Override
        public int delete(Uri uri, String s, String[] strings) {
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
            return 0;
        }
    }
  • 相关阅读:
    [转载]ASIHTTPRequest使用介绍
    mac下显示隐藏文件的方法
    Proguard 4.x error java.lang.ArrayIndexOutOfBoundsException
    【iPhone开发】说说Xcode4中xib绑定的原理
    【转】ObjectiveC 属性特性(assign , retain , copy , readonly , readwrite , atomic , nonatomic)
    xcode 4.X无法连接svn的问题
    .NET System.Timers.Timer的原理和使用(开发定时执行程序)
    步步为营 SharePoint 开发学习笔记系列 七、SharePoint Timer Job 开发
    步步为营 SharePoint 开发学习笔记系列 六、EditorPart开发
    C# FileSystemWatcher 在监控文件夹和文件时的用法
  • 原文地址:https://www.cnblogs.com/sunrise-hs/p/5665338.html
Copyright © 2011-2022 走看看