最主要的是需要自定义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];
-
}
-
}
-
- }
其中用到了一些常量如下:
点击(此处)折叠或打开
-
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的代码如下:
点击(此处)折叠或打开
-
public class MyCursor extends AbstractCursor{
-
private static final String TAG = "MyCursor";
-
-
private String[] columnNames= null;//构建cursor时必须先传入列明数组以规定列数
-
-
/**
-
* 数据区域
-
*/
-
//所有的数据
-
private ArrayList<ArrayList<String>> allDatas= new ArrayList<ArrayList<String>>();//在构造的时候填充数据,里层数据的size=columnNames.leng
-
//当前一项的数据
-
private ArrayList<String> oneLineData= null;//onMove时填充
-
-
public MyCursor(){
-
//必须构建完整列信息
-
columnNames = new String[]{"id","name"};
-
}
-
-
/**
-
* 加载我们的数据信息
-
*/
-
public void updateAllData(Collection<ArrayList<String>> data){
-
mPos = -1;
-
allDatas.clear();
-
allDatas.addAll(data);
-
}
-
-
/**
-
* 加载我们的数据信息
-
*/
-
public void updateUserData(ArrayList<String> data){
-
mPos = -1;
-
allDatas.clear();
-
allDatas.add(data);
-
}
-
-
/**
-
* 获取当前行对象,为一个oneLineDatastring[]
-
*/
-
-
@Override
-
public boolean onMove(int oldPosition, int newPosition) {
-
if(newPosition< 0 || newPosition >= getCount()){
-
oneLineData= null;
-
return false;
-
}
-
-
int index = newPosition ;
-
if(index< 0 || index >= allDatas.size()){
-
return false;
-
}
-
oneLineData= allDatas.get(index);
-
return super.onMove(oldPosition,newPosition);
-
}
-
-
/**
-
*获取游标行数
-
*/
-
@Override
-
public int getCount() {
-
return allDatas.size();
-
}
-
-
/**
-
* 获取列名称
-
*/
-
@Override
-
public String[] getColumnNames() {
-
return columnNames;
-
}
-
-
-
@Override
-
public String getString(int column) {
-
if(oneLineData== null){
-
return null;
-
}
-
return oneLineData.get(column);
-
}
-
-
@Override
-
public int getInt(int column) {
-
Object value = getString(column);
-
try{
-
return value != null? ((Number) value).intValue() : null;
-
} catch(ClassCastException e) {
-
if(value instanceof CharSequence) {
-
try{
-
return Integer.valueOf(value.toString());
-
} catch(NumberFormatException e2) {
-
Log.e(TAG,"Cannotparse int value for "+ value + "at key "+ column);
-
return 0;
-
}
-
} else{
-
Log.e(TAG,"Cannotcast value for "+ column + "to a int: "+ value, e);
-
return 0;
-
}
-
}
-
}
-
-
/**
-
* 以下参考getInt(int column)
-
*/
-
-
@Override
-
public short getShort(int column) {
-
return 0;
-
}
-
-
@Override
-
public long getLong(int column) {
-
return 0;
-
}
-
-
@Override
-
public float getFloat(int column) {
-
return 0;
-
}
-
-
@Override
-
public double getDouble(int column) {
-
return 0;
-
}
-
-
@Override
-
public boolean isNull(int column) {
-
return false;
-
}
- }