一
自定义ContentProvider
步骤:1.创建一个类,继承Contentprovider
2.实现所有的抽象方法;onCreate()
getType()
query()
update()
delete()
ContentResolver对象调用的query() 、insert() 、update()、delete() 就是 ContentProvider类中的重写后的query() 、insert() 、update()、delete() 方法。
3.定义ContentProvider的Uri,
4. UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
静态代码块 static{
//添加要 匹配的URI
<span style="white-space:pre"> </span>参数1:authority
<span style="white-space:pre"> </span>参数2:资源路径:表名
<span style="white-space:pre"> </span>参数3:该uri匹配成功的返回码code
//完整路径Uri:content://com.example.day17demo01provider.db.StuProvider/javaScore
<span style="white-space:pre"> </span>matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME, 1);
//模糊匹配:id--->#数值
<span style="white-space:pre"> </span>matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME+"/#", 2);
//模糊匹配:name---》*文本
matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME+"/*", 3);
}
//实例化ContentProvider的时候调用---》不要做耗时操作
@Override
public boolean onCreate() {
db=new DBHelper(getContext()).getWritableDatabase();
return false;
}
//查找数据--->查找SQLite---》SQLiteDataBase
//参数1:uri 参数2:要查找的列 参数4:查找条件 参数5:条件中占位符的值 参数6:排序
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//把传进来的uri进行匹配---》定位一条数据还是多条数据
int code = matcher.match(uri);
Cursor cursor = null;
switch (code) {
case 1://全部
cursor = db.query(DBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
break;
case 2://指定id
//取出uri中的id
long id = ContentUris.parseId(uri);
cursor = db.query(DBHelper.TABLE_NAME, projection, DBHelper.JAVA_ID+" = ?", new String[]{id+""}, null, null, sortOrder);
break;
case 3://指定name
//取出name值
String name=uri.getLastPathSegment();
//查找条件---》name的值--》模糊匹配
cursor=db.query(DBHelper.TABLE_NAME, projection, " name like '%"+name+"%'", null, null, null, sortOrder);
break;
default:
break;
}
return cursor;
}
//获取类型--->name、没有值匹配---》1、3---》多条数据
//2:单条数据
@Override
public String getType(Uri uri) {
String type=null;
int code = matcher.match(uri);
switch (code) {
case 1://多条数据
case 3:
type="vnd.android.cursor.dir/vnd."+AUTHORITY+"."+DBHelper.TABLE_NAME;
break;
case 2:
type="vnd.android.cursor.item/vnd."+AUTHORITY+"."+DBHelper.TABLE_NAME;
break;
default:
break;
}
return type;
}
//插入数据
@Override
public Uri insert(Uri uri, ContentValues values) {
int code = matcher.match(uri);
Uri withAppendedId =null;
if(code==1){
long id = db.insert(DBHelper.TABLE_NAME, null, values);
withAppendedId = ContentUris.withAppendedId(uri, id);
}
return withAppendedId;
}
//删除
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int rawNum =0;
int code = matcher.match(uri);
if(code==2){//包含有数值--》id
long id = ContentUris.parseId(uri);//删除条件
//数据库的删除操作
rawNum = db.delete(DBHelper.TABLE_NAME, DBHelper.JAVA_ID+" = ?", new String[]{id+""});
}
return rawNum;
}
//更新
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int code = matcher.match(uri);
switch (code) {
case 1://没有指定name、id
db.update(DBHelper.TABLE_NAME, values, selection, selectionArgs);
break;
case 2://id
//取出id
long id = ContentUris.parseId(uri);
//根据id去更新数
db.update(DBHelper.TABLE_NAME, values, DBHelper.JAVA_ID+" = ?", new String[]{id+""});
break;
case 3://name
//取出name
String name = uri.getLastPathSegment();
//根据name去更新
db.update(DBHelper.TABLE_NAME, values, DBHelper.JAVA_NAME+" like '%"+name+"%'", null);
break;
default:
break;
}
return 0;
}
}