一般情况我们使用ContentProvider主要访问数据库数据,但有时也可能需要访问非数据库数据,如XML或者本地数据,那就需要做一下特殊处理
最主要的是需要自定义Cursor,因为ContentProvider的query方法,只能返回Cursor引用。直接上代码,咱们边看边说
-
public class MyContentProvider extends ContentProvider {
-
-
//操作URI的类
-
public static final UriMatcher uriMatcher;
-
//为UriMatcher添加自定义的URI
-
static{
-
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
-
uriMatcher.addURI(ProviderConstant.AUTHORITIES,"/user",
-
ProviderConstant.INCOMING_USER_COLLECTION);
-
uriMatcher.addURI(ProviderConstant.AUTHORITIES,"/user/#",
-
ProviderConstant.INCOMING_USER_SINGLE);
-
}
-
-
private HashMap<String,ArrayList<String>> allDatas= new HashMap<String,ArrayList<String>>();
-
-
-
private MyCursor cursor;
-
-
public MyContentProvider(){
-
cursor = new MyCursor();
-
loadAllData();
-
}
-
-
-
/**
-
* 加载我们的数据信息
-
*/
-
public void loadAllData(){
-
allDatas.clear();
-
for(int pos=0; pos < 5;pos++){//填充5个学生数据
-
ArrayList<String> dataList = new ArrayList<>();
-
dataList.add(pos+"");//id
-
dataList.add("name"+pos);//name
-
allDatas.put(pos+"",dataList);
-
}
-
}
-
-
/**
-
* 删除数据
-
*/
-
@Override
-
public int delete(Uri uri, String selection, String[] selectionArgs) {
-
System.out.println("delete");
-
int count = 0;
-
String id = getUserId(selection, selectionArgs);
-
if(allDatas.containsKey(id)){
-
allDatas.remove(id);
-
count = 1;
-
}
-
return count;
-
}
-
-
/**
-
* 数据库访问类型
-
*/
-
@Override
-
public String getType(Uri uri) {
-
System.out.println("getType");
-
//根据用户请求,得到数据类型
-
switch (uriMatcher.match(uri)) {
-
case ProviderConstant.INCOMING_USER_COLLECTION:
-
return ProviderConstant.CONTENT_TYPE;
-
case ProviderConstant.INCOMING_USER_SINGLE:
-
return ProviderConstant.CONTENT_TYPE_ITEM;
-
default:
-
throw new IllegalArgumentException("UnKnown URI"+uri);
-
}
-
}
-
-
/**
-
* 插入数据
-
*/
-
@Override
-
public Uri insert(Uri uri, ContentValues values) {
-
ArrayList<String> data = new ArrayList<>();
-
int id =values.getAsInteger(ProviderConstant.ID);
-
String name = values.getAsString(ProviderConstant.NAME);
-
data.add(id+"");
-
data.add(name);
-
allDatas.put(id+"", data);
-
return uri;
-
}
-
-
/**
-
* 创建ContentProvider时调用的回调函数
-
*/
-
@Override
-
public boolean onCreate() {
-
System.out.println("onCreate");
-
return true;
-
}
-
-
/**
-
* 查询数据库
-
*/
-
@Override
-
public Cursor query(Uri uri, String[] projection, String selection,
-
String[] selectionArgs, String sortOrder) {
-
//判断用户请求,查询所有还是单个
-
switch(uriMatcher.match(uri)){
-
case ProviderConstant.INCOMING_USER_COLLECTION:
-
cursor.updateAllData(allDatas.values());
-
break;
-
case ProviderConstant.INCOMING_USER_SINGLE:
-
String id = uri.getLastPathSegment();
-
cursor.updateUserData(allDatas.get(id));
-
break;
-
}
-
return cursor;
-
}
-
-
/**
-
* 更新数据库
-
*/
-
@Override
-
public int update(Uri uri, ContentValues values, String selection,
-
String[] selectionArgs) {
-
System.out.println("update");
-
String id = getUserId(selection, selectionArgs);
-
int count=0;
-
if(allDatas.containsKey(id)){
-
String name = values.getAsString(ProviderConstant.NAME);
-
//index取决于name所在的列
-
allDatas.get(id).set(cursor.getColumnIndex(ProviderConstant.NAME), name);
-
count = 1;
-
}
-
return count;
-
}
-
-
/**
-
* 获取关键信息,用户ID
-
* @param selection
-
* @param selectionArgs
-
* @return
-
*/
-
private String getUserId(String selection, String[] selectionArgs){
-
if(selectionArgs == null || selectionArgs.length == 0){
-
//id 包含在selection里
-
return selection.substring(selection.indexOf("=")+1).trim();
-
}else{
-
return selectionArgs[0];
-
}
-
}
-
-
}
以上是自定义contentProvider,网上有很多例子和说明,不在详述,我们主要看一下query,我返回自定义Cursor。
其中用到了一些常量如下:
-
public class ProviderConstant {
-
//URI的指定,此处的字符串必须和声明的authorities一致
-
public static final String AUTHORITIES = "com.zy.sports.app.MyContentProvider";
-
-
//访问该ContentProvider的URI
-
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/user");
-
-
//该ContentProvider所返回的数据类型的定义
-
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.myprovider.user";
-
public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd.myprovider.user";
-
-
-
//访问表的所有列
-
public static final int INCOMING_USER_COLLECTION = 1;
-
//访问单独的列
-
public static final int INCOMING_USER_SINGLE = 2;
-
-
-
public static final String ID = "id";
-
public static final String NAME = "name";
-
-
}
自定义Cursor的代码如下:
我们看到Cursor是一个接口,我们只需要继承AbstractCursor或者他的子类来重定义Cursor的方法。大家可以参考,欢迎多提意见
阅读(2353) | 评论(0) | 转发(0) |