這只是我在學(xué)Android過程中對于所學(xué)知識的鞏固和方便日后查詢的學(xué)習(xí)筆記斧抱,能幫助到有需要的和我一樣的初學(xué)者就更好了
ContentProvider是用來在應(yīng)用件共享數(shù)據(jù)的耸采,直接暴露自己的數(shù)據(jù)庫十分危險,而ContentProvider可以向外提供自己同意讓其他應(yīng)用訪問的數(shù)據(jù),也可訪問其他應(yīng)用共享的數(shù)據(jù)
ContentProvider用法和SQLiteDatabase很像忠怖,但參數(shù)為Uri對象再借助getContentProvider獲取ContentProvider對象來完成CRUD操作
Uri對象的參數(shù)格式為
content://packagename+.provider/tablename
或者
content://authority/path
packagename+.provider為authority ; tablename為path
例如
content://com.example.app.provider/table1/id
通配符
- :任意長度任意字符
content://com.example.app.provider/ 匹配任意表
:任意長度任意數(shù)字
**content://com.example.app.provider/table1/# 匹配表1中任意行數(shù)據(jù)
···
Uri uri=Uri.parse("content://com.example.app.provier/table1/1")
···
有了Uri對象即可做相應(yīng)的動作
增
ContentValues values=newContentValues();
values.put("column1" ,"十萬個為什么");
values.put("column2" ,1);
getContentProvider().insert(uri ,values);//uri對象指定了具體哪張表
查
Cursor cursor=getContentProvider().query(
uri,
column,
selection,
selectionArgs,
sortOrder );
再配合游標(biāo)來遍歷即可
刪
getContentProvider().delete(uri ,selection ,selectionArgs);
改
ContentValues values=new ContentValues();
values.put("colunm1" ,"十萬個冷笑話");
values.put("column2" ,2);
getContentProvider().update(
uri,
"column1=? and column2=? ,
new String[]{"十萬個為什么" ,1});
//uri指定表中column1為十萬個為什么column2為1的元素的column1、column2修改
創(chuàng)建自己的ContentProvider
創(chuàng)建一個MyProvider類繼承ContentProvider類并重寫全部6個抽象方法
-
boolean onCreate()
初始化抄瑟、與此完成數(shù)據(jù)庫的創(chuàng)建于升級凡泣,返回true為成功 -
Cursor query()
返回查詢結(jié)果 -
Uri insert()
返回表示此新記錄的Uri對象 -
int update()
返回更新的行數(shù) -
int deleted()
返回刪除的行數(shù) -
String getType()
根據(jù)傳入的Uri來返回響應(yīng)的MIME類型
配合UriMatcher的match()方法即可將任意Uri對象傳入并返回此Uri對象對應(yīng)的自定義代碼
UriMacher類似與一個文本過濾器,不實用它的話再匹配時我們要寫很多東西皮假,如:
public static final String addr="content://"+AUTHORITY+"/"+TABLE+"/"+"#";
而使用UriMatcher.matche(uri)則傳入Uri對象直接幫我們鎖定與具體的表單地址也就是"content://"后的內(nèi)容相匹配的自定義代碼鞋拟,從而無需我們對傳入的Uri對像進行解析
此處引用自學(xué)時書上的例子(為一個含有數(shù)據(jù)庫的項目創(chuàng)建內(nèi)容提供器)
public class MyProvider extends ContentProvider{
public static final int BOOK_DIR=0;
public static final int BOOK_ITEM=1;
public static final int CATEGORY_DIR=2;
public static final int CATEGORY_ITEM=3;
public static final String AUTHORITY="com.example.databasetest.provider";
private static UriMatcher urimatcher;
private MyHelper dbhelper;
static{
urimatcher=new UriMatcher(UriMatcher.NO_MATCH);
urimatcher.addURI(AUTHORITY ,"book" ,BOOK_DIR);//book表全部元素
urimatcher.addURI(AUTHORITY ,"book/#" ,BOOK_ITEM);//book表具體元素
urimatcher.addURI(AUTHORITY ,"category" ,CATEGORY_DIR);
urimatcher.addURI(AUTHORITY ,"category/#" ,CATEGORY_ITEM);
//通過addURI()方法將自定義代碼BOOK_DIR直接指向"com.example.databasetest.provider/book";其余同理
}
@override
public boolean onCreate(){
dbhelper=new MyHelper(getContext() ,"Library" ,null ,1);
returen true;
}
@overrid
public Cursor query(Uri uri ,String[] column ,String selection ,
String[] selectionArgs ,String sortorder){
//查:通過對傳入的uri解析鎖定表單惹资,再借助項目自身的數(shù)據(jù)庫查詢并返回cursor結(jié)果
SQLiteDatabase db=dbhelper.getReadableDatabase();
Cursor cursor =null;
switch(UriMacher.match()){
case BOOK_DIR:
cursor =db.query("Book" ,column ,selection ,selectionArgs
,null ,null ,sortorder);
break;
case BOOK_ITEM:
String bookId=uri.getPathSegment().get(1);
//此方法將"content://authority/path/id"以"/"分隔贺纲;path為第0位 ,id為第1位
cursor=db.query("Book" ,column ,"id=?",new String[]{"bookId"}
,null ,null ,sortorder);
break;
case CATEGORY_DIR:
cursor =db.query("Category" ,column ,selection ,selectionArgs
,null ,null ,sortorder);
break;
case CATEGORY_ITEM:
String categoryId=uri.getPathSegment().get(1);
cursor=db.query("Category" ,column ,"id=?",new String[]{"cakegoryId"}
,null ,null ,sortorder);
break;
default:
break;
}
return cursor;
}
@override
public Uri insert(Uri uri ,ContentValues values ){
//增:通過解析傳入的uri再借助自身的數(shù)據(jù)庫完成添加并返回此新增元素的uri
SQLiteDatabase db=dbhelper.getWritableDatabse();
Uri returnUri =null;
switch(UriMacher.match(uri)){
case BOOK_DIR:
case BOOK_ITEM;
long newBookId=db.insert("Book" ,null ,values);
returnUri=Uri.parse("content://"+AUHORITY+"/book/"+newBookId);
//newBookId保存了新存入的元素的Id,然后將此元素的完整信息解析為Uri對象并返回
break;
case CATEGORY_DIR:
case CATEGORY_ITEM;
long newCategoryId=db.insert("Category" ,null ,values);
returnUri=Uri.parse("content://"+AUHORITY+"/category/"+newCategoryId);
break;
default:
break;
}
return returenUri;
}
@override
public int delete(Uri uri ,String selection ,String[] selectionArgs){
//刪:返回刪除的行數(shù)
SQLiteDatabase db=dbhelper.getWritableDatabase();
int deleteRows =0;
switch(UriMacher.match(uri)){
case BOOK_DIR:
deleteRows=db.delete("Book" , selection ,selectionArgs);
break;
case BOOK_ITEM:
String bookId=uri.getPathSegment().get(1);
deleteRows=db.delete("Book" ,"id=?" ,new String[]{bookId});
break;
case CATEGORY_DIR:
deleteRows=db.delete("Category" , selection ,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId=uri.getPathSegment().get(1);
deleteRows=db.delete("Category" ,"id=?" ,new String[]{categoryId});
break;
default:
break;
}
returen deleteRows;
}
@override
public int update(Uri uri ,ContentValues values ,String selection ,
String[] selectionArgs ){
//改:通過解析uri借助自身的數(shù)據(jù)庫來完成并返回更新的行數(shù)
SQLiteDatabse db=dbhelper.getWritableDatabse();
int updateRows=0;
switch(UriMatcher.match(uri)){
case BOOK_DIR:
updateRows=db.update("Book" ,values ,selection ,selectionArgs);
break;
case BOOK_ITEM:
String bookId=uri.getPathSegment().get(1);
updateRows=db.update("Book" ,values ,"id=?" ,new String[]{bookId});
break;
case CATEGORY_DIR:
updateRows=db.update("Category" ,values ,selection ,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId=uri.getPathSegment().get(1);
updateRows=db.update("Category" ,values ,"id=?" ,new String[]{category});
break;
default:
break;
}
return updateRows;
}
@override
public String getType(Uri uri){
switch(UriMatcher.match(uri)){
case BOOL_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com,example.databaetest.provider.book";
case CATEGORY_DIR::
return "vnd.android.cursor.item/vnd.com,example.databaetest.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com,example.databaetest.provider.category";
}
return null;
}
}
getType()中的返回值格式為
全部: vnd.android.cursor.dir/vnd.authority.path
具體: vnd.android.cursor.item/vnd.authority.path
訪問自定義內(nèi)容提供器
- 增
Uri uri=Uri.parse("content://cpm.example.databasetest.provider/book");
ContentValues values=new ContentValues();
values.put(key ,data); //根據(jù)自己需求填寫鍵與值
value.put(key2 ,data2);
Uri newuri=getContentProvider().insert(uri ,values);
String newId=newuri.getPathSegement().get(1);
//此newuri是添加的元素的Id布轿,下面可以用它對這添加的元素進行操作
- 查
Uri uri=Uri.parse("content://cpm.example.databasetest.provider/book");
Cursor cursor=getContentProvider().query(uri ,null ,null ,null ,null);
if(cursor!=null){
do{
String name =cursor.getString(cursor.getColumnIndex("name"));
......
}while(cursor.moveToNext());
}
cursor.close();
- 刪
Uri uri =Uri.parse("content://cpm.example.databasetest.provider/book/"+newId);
//通過之前保存的newId鎖定了剛剛添加的元素
getContentProvider().delete(uri ,null ,null);
- 改
Uri uri =Uri.parse("content://cpm.example.databasetest.provider/book/"+newId);
ContentValues values=new ContentValues();
values.put(key ,data);
......
getContentProvider().update(uri ,values ,null ,null);