Android自助餐之内容提供者ContentProvider使用

Android自助餐之内容提供者ContentProvider使用

查看全套目录

下载完整源代码

创建提供者Module

此例中Module为SampleOfContentProvider

  1. 创建Provider类
    此例中类名InfoProvider
  2. 在Manifest中声明

    <provider
    android:exported="true"
    android:authorities="com.xmh.sampleofcontentprovider"
    android:name="com.xmh.sampleofcontentprovider.InfoProvider">
    </provider>
  3. 明确URI
    定义静态字符串URI_PROVIDER="com.xmh.sampleofcontentprovider"作为访问URI。
  4. 在静态代码块中初始化Uri匹配器

    matcher= new UriMatcher(UriMatcher.NO_MATCH);
    matcher.addURI(URI_PROVIDER,"insert",INSERT);
    matcher.addURI(URI_PROVIDER,"delete",DELETE);
    matcher.addURI(URI_PROVIDER,"update",UPDATE);
    matcher.addURI(URI_PROVIDER,"query",QUERY);
    matcher.addURI(URI_PROVIDER,"query/#",QUERY_ONE);
  5. 在onCreate时初始化数据库操作对象

    database= getContext().openOrCreateDatabase(SQL_NAME, Context.MODE_PRIVATE, null);
    database.execSQL("create table if not exists "+TABLE_NAME+"(id varchar(10),name varchar(10),age int)");
    database.execSQL("INSERT INTO "+TABLE_NAME+" VALUES ('001','aa', 10)");
    database.execSQL("INSERT INTO "+TABLE_NAME+" VALUES ('002','bb', 20)");
    //此处临时创建一个数据库
  6. 重写query方法

    //匹配URI
    if(matcher.match(uri)==QUERY){
    //执行查询语句并返回游标
    Cursor cursor = database.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
    //因为游标还要使用,注意不要关闭数据库
    return cursor;
    }else if(matcher.match(uri)==QUERY_ONE){
    //根据age查询一条数据
    long age= ContentUris.parseId(uri);
    Cursor cursor = database.query(TABLE_NAME, projection, "age=?", new String[]{age + ""}, null, null, sortOrder);
    return cursor;
    }else {
    Log.e("xmh","query-error:"+uri);
    }
    return null;
  7. 重写insert方法

    //匹配URI
    if(matcher.match(uri)==INSERT){
    database.insert(TABLE_NAME,null,values);
    }else {
    Log.e("xmh","insert-error:"+uri);
    }
    return null;
  8. 重写delete方法

    //匹配URI
    if(matcher.match(uri)==DELETE){
    //执行删除并返回受影响行数
    return database.delete(TABLE_NAME, selection, selectionArgs);
    }else {
    Log.e("xmh","delete-error:"+uri);
    }
    return 0;
  9. 重写update方法

    //匹配UIR
    if(matcher.match(uri)==UPDATE){
    //执行修改并返回受影响行数
    return database.update(TABLE_NAME,values,selection,selectionArgs);
    }else {
    Log.e("xmh","update-error:"+uri);
    }
    return 0;
  10. 重写getType方法

    if(matcher.match(uri)==QUERY){
    //dir表示一组数据
    return "vnd.android.cursor.dir/beanname";
    }else if(matcher.match(uri)==QUERY_ONE){
    //item表示一项数据
    return "vnd.android.cursor.item/beanname";
    }else{
    Log.e("xmh","type-error:"+uri);
    }
    return null;

创建内容使用者Module

此例中Module为SampleOfContentProviderOther,以下方法均在Activity中测试
1. 查询数据

//得到内容提供中间件
ContentResolver resolver = getContentResolver();
//生成URI
Uri uri=Uri.parse("content://com.xmh.sampleofcontentprovider/query");
//请求获取游标
Cursor cursor = resolver.query(uri, null, null, null, null);
//处理查询结果
while(cursor.moveToNext()){
String id=cursor.getString(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.e("xmh","id="+id+",name="+name+"age="+age);
}
//关闭游标
cursor.close();

1. 删除数据

//得到内容提供中间件
ContentResolver resolver = getContentResolver();
//生成URI
Uri uri=Uri.parse("content://com.xmh.sampleofcontentprovider/delete");
//请求删除
int delete = resolver.delete(uri, "id = ?", new String[]{"001"});

1. 更新数据

//得到内容提供中间件
ContentResolver resolver=getContentResolver();
//生成URI
Uri uri=Uri.parse("content://com.xmh.sampleofcontentprovider/update");
//请求修改
ContentValues contentValues = new ContentValues();
contentValues.put("age",50);
int count=resolver.update(uri, contentValues, "id=?", new String[]{"002"});

1. 插入数据

//得到中间件
ContentResolver resolver=getContentResolver();
//生成URI
Uri uri=Uri.parse("content://com.xmh.sampleofcontentprovider/insert");
//请求插入数据
ContentValues contentValues = new ContentValues();
contentValues.put("name", "beda");
resolver.insert(uri, contentValues);

1. 查询单条数据

//得到中间件
ContentResolver resolver=getContentResolver();
//生成URI
Uri uri=Uri.parse("content://com.xmh.sampleofcontentprovider/query/10");
//请求获取游标
Cursor cursor = resolver.query(uri, null, null, null, null);
//处理查询结果
while(cursor.moveToNext()){
String id=cursor.getString(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.e("xmh","id="+id+",name="+name+"age="+age);
textView.setText("id="+id+",name="+name+",age="+age);
}
//关闭游标
cursor.close();

发布了69 篇原创文章 · 获赞 55 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xmh19936688/article/details/50551099