Content Provider

Android學(xué)習(xí)筆記(3)————Android四大組件之三(Content Provider)(代碼示例待補(bǔ)全)


Android中ContentProvider組件詳解

【Android】Uri、UriMatcher、ContentUris詳解

ContentProvider與ContentResolver使用

安卓ContentResolver的query參數(shù)說明


Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);

參數(shù):

uri, 用于 Content Provider 查詢的 URI,也就是說從這個(gè) URI 中獲取數(shù)據(jù)。

例如:

Uri uri = Contacts.People.CONTENT_URI;//聯(lián)系人列表URI。

projection, 用于標(biāo)識(shí) uri 中有哪些 columns 需要包含在返回的 Cursor 對(duì)象中。

例如:

//待查詢的columnsString[] projection = {

Contacts.PeopleColumns.NAME,

Contacts.PeopleColumns.NOTES

};

selection, 作為查詢的過濾參數(shù)(過濾出符合 selection 的數(shù)據(jù)),類似于 SQL 中 Where 語句之后的條件選擇。

例如:

String selection = Contacts.People.NAME + “=?” //查詢條件

selectionArgs, 查詢條件參數(shù),配合 selection 參數(shù)使用。

例如:

String[] selectionArgs = {“Braincol”, “Nixn.dev”};//查詢條件參數(shù)

sortOrder,查詢結(jié)果的排序方式(按查詢列( projection 參數(shù)中的 columns )中的某個(gè) column )排序)。

例如:

String sortOrder = Contacts.PeopleColumns.NAME; //查詢結(jié)果的排序方式(按指定的查詢列排序)

返回值:

一個(gè)包含指定數(shù)據(jù)的 Cursor 對(duì)象。

示例:

Uri uri = Contacts.People.CONTENT_URI;

String[] projection = { Contacts.PeopleColumns.NAME,

Contacts.PeopleColumns.NOTES

};

String selection = Contacts.PeopleColumns.NAME + "=?";

String[] selectionArgs = { "Braincol","Nixn.dev" };

String sortOrder = Contacts.PeopleColumns.NAME;

//使用 managedQuery 獲取 Contacts.People 這個(gè) ContentProvider 的 Cursor。

Cursor cursor = managedQuery(uri, projection, selection, selectionArgs,sortOrder);

上述示例的含義就是:在聯(lián)系人列表中查詢 NAME 為 Braincol 和 Nixn.dev 兩個(gè)聯(lián)系人的 "NAME" 和 "NOTES" 信息,并且將這些信息按照名字( NAME )排序,最后將排序之后的結(jié)果包裝在一個(gè) Cursor 對(duì)象中返回。

android.content.ContentValues保存將插入的單一記錄的值。ContentValues是一個(gè)鍵/值對(duì)字典。

例如:

//將記錄填充到ContentValues

ContentValues values = new ContentValues();

values.put("title","New note");

values.put("note","This is a new note");

//告訴android.content.ContentResolver使用URI插入記錄

ContentResolver contentResolver = new ContentResolver(); //獲取ContentResolver的引用

Uri newUri = contentResolver.insert(Notepad.notes.CONTENT_URI,values);//返回的newUri的結(jié)構(gòu):Notepad.notes.CONTENT_URI/new_id

//獲取對(duì)文件輸出流的引用

OutputStream outStream = contentResolver.openOutputStream(newUri);

someSourceBitmap.compress(Bitmap.CompressFormat.JEPG, 50, outStream);

outStream.close();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容