1然评、ContentProvider配置
1.1 自定義類繼承于ContentProvider档冬,實現(xiàn)要求的方法鹉动。
Androidstudio:new->other->ContentProvider
1.2 在配置文件中通過provider標簽配置含衔,通過android:name屬性指定待配置的類矾瘾,通過android:authorities屬性授權懈玻,指定當前內容提供者的URI標識巧婶,必須唯一。
<provider
android:name = ".MyContentProvider"
android:authorities = "com.example.mycontentprovider"
android:enabled = "true"
android:exported = "true"></provider>
1.3 在別的應用中想要獲取ContentProvider的內容涂乌,就需要用到ContentResolver類艺栈。
ContentResolver resolver;
....
//獲取ContentResolver對象
resolver = getContentResolver();
// resolver.query();
resolver.insert();//這里的方法最終會調用目標ContentProvider中的同名insert方法
// resolver.delete();
// resolver.update();
2、ContentProvider中的常用方法
2.1 onCreate():
用于在ContentProvider創(chuàng)建時調用骂倘,對于ContentProvider而言眼滤,無論數(shù)據(jù)的來源是什么,他都認為是種表历涝,然后把數(shù)據(jù)組織成表格诅需,以操作數(shù)據(jù)庫表的形式操作這些數(shù)據(jù)。所以要在OnCreate方法里新建一張數(shù)據(jù)庫表荧库。
//1堰塌、直接傳文件名稱是默認存儲在私有目中,別的應用只有通過ContentProvider才能訪問
SQLiteOpenHelper helper = new SQLiteOpenHelper(getContext(), "stu.db", null, 1) {
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table info_tb (_id integer primary key autoincrement," +
"name varchar(20)," +
"age integer," +
"gender varchar(2))";
db.execSQL(sql);
}
2.2 insert():
用于向ContentProvider的提供方中插入數(shù)據(jù)分衫,需要定義插入的數(shù)據(jù)场刑,那么先來看一下insert方法的參數(shù):
Uri uri=resolver.insert(Uri.parse("..."),values);
ContentResolver.insert()方法的返回值是一個Uri對象,方法里面的參數(shù)有兩個
- 參數(shù)1:URL對象,content://authorities/path蚪战,如:content://com.example.mycontentprovider
- 其中牵现,Uri.parse方法是用于將字符串解析成Uri對象的方法铐懊。
- 參數(shù)2:ContentValues對象,這是一種只能存儲基本數(shù)據(jù)類型的機制瞎疼,并且是以鍵值對的方式存儲的
上面提到需要定義插入的數(shù)據(jù)是ContentValues對象科乎,那么首先需要先實例化一個ContentValues類的對象:
ContentValues values=new ContentValues();
這里要注意的是,存儲的數(shù)據(jù)是以鍵值對的形式存在的贼急,這里的鍵名應該和上面定義的數(shù)據(jù)庫表的列名一致:
values.put("name",name);
values.put("age",age);
values.put("gender",gender);
注意:這里的insert()方法實際上調用了ContentProvider中的insert方法茅茂,所以我們要重寫ContentProvider的insert()方法,以使得我們傳過去的values能夠插入到數(shù)據(jù)庫表中太抓,關鍵部分代碼如下:
@Override
public Uri insert(Uri uri, ContentValues values) {
long id=db.insert("info_tb",null,values);
return ContentUris.withAppendedId(uri,id);//該方法把id追加到Uri后面
}
這里的返回值實際上還會傳回給ContentResolver空闲,可以選則用ContentUris這個工具類的withAppendedId(Uri contentUri, long id)方法,這個方法負責把id和contentUri連接成一個新的Uri走敌。比如在我們這個例子當中是這么使用 的:ContentUris.withAppendedId (uri,id)碴倾。如果Id為100的話,那么現(xiàn)在的這個Uri的內容就是:
content://com.example.mycontentprovider/100
在ContentResolver中悔常,可以使用ContentUris工具類的parseId()方法來把contenUri 后邊的id解析出來影斑。
2.3 query():
首先给赞,重寫ContentProvider中的query方法机打,因為實際上ContentResolver方法中的query方法是調用的這個方法:
Cursor c=db.query("info_tb",projection,selection,selectionArgs,null,null,sortOrder);
再來看下ContentResolver方法中的query方法:
Cursor c=resolver.query(Uri.parse("content://com.example.mycontentprovider"),
null,null,null,null);
這個方法參數(shù)有很多:
- 參數(shù)1:要訪問的ContentProvider的唯一標識符
- 數(shù)2:查詢哪些列(如果查詢所有列,就是null)
- 參數(shù)3:查詢條件(where)
- 參數(shù)4:條件值重寫ContentProvider中的query方法片迅,因為實際上ContentResolver方法中的query方法是調用的這個方法:
- 參數(shù)5:按什么排序
要把查詢到的結果展示在ListView残邀,需要定義適配器,這里的查詢結果是存儲在Cursor中的柑蛇,所以采用SimpleCursorAdapter這個游標適配器:
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, R.layout.item, c,
new String[]{"_id", "name", "age", "gender"},
new int[]{R.id.id_item, R.id.name_item, R.id.age_item, R.id.gender_item}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mListView.setAdapter(adapter);
SimpleCursorAdapter的參數(shù)有很多:
- 參數(shù)1:當前環(huán)境
- 參數(shù)2:ListView所引用的布局(裝載數(shù)據(jù)的列表項)
- 參數(shù)3:數(shù)據(jù)源
- 參數(shù)4:Cursor里面數(shù)據(jù)每一列的列名組成的字符串數(shù)組
- 參數(shù)5:對應列的id(要把什么類型的數(shù)據(jù)放入什么地方)形成的數(shù)組
- 參數(shù)6:設置及時把更新過的數(shù)據(jù)顯示在列表上
delete():
首先芥挣,先重寫ContentProvider中的delete方法,因為實際上ContentResolver方法中的delete方法是調用的這個方法:
int result=db.delete("info_tb",selection,selectionArgs);
return result;
這里的參數(shù)有三個:
參數(shù)1:表名
參數(shù)2:條件
參數(shù)3:條件值組成的數(shù)組
Result的返回值為1耻台,說明刪除成功空免,否則說明刪除失敗
接下來再來看下ContentResolver方法中的delete方法
resolver.delete(Uri.parse("content://com.example.mycontentprovider"), "_id=?", new String[]{_id});
這里的參數(shù)有三個:
參數(shù)1:ContentProvider的唯一標識符
參數(shù)2:條件
參數(shù)3:條件值組成的數(shù)組