1.獲取聯(lián)系人姓名
一個簡單的例子,這個函數(shù)獲取設(shè)備上所有的聯(lián)系人ID和聯(lián)系人NAME。
[java]?view plaincopy
public?void?fetchAllContacts()?{?? ????ContentResolver?contentResolver?=?this.getContentResolver();?? ????Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????????????null,?null,?null,?null);?? ????cursor.getCount();?? ????while(cursor.moveToNext())?{?? ????????System.out.println(cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)));?? ????????System.out.println(cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)));?? ????}?? ????cursor.close();?? }??
執(zhí)行結(jié)果:
[java]?view plaincopy
11-05?14:13:09.987:?I/System.out(4692):?13?? 11-05?14:13:09.987:?I/System.out(4692):?張三?? 11-05?14:13:09.987:?I/System.out(4692):?31?? 11-05?14:13:09.987:?I/System.out(4692):?李四??
解釋:
[java]?view plaincopy
ContentResolver?contentResolver?=?this.getContentResolver();??
this在這里指的是MainActivity,ContentResolver直譯為內(nèi)容解析器,什么東東?Android中程序間數(shù)據(jù)的共享是通過Provider/Resolver進(jìn)行的。提供數(shù)據(jù)(內(nèi)容)的就叫Provider,Resovler提供接口對這個內(nèi)容進(jìn)行解讀。
在這里,系統(tǒng)提供了聯(lián)系人的Provider,那么我們就需要構(gòu)建一個Resolver來讀取聯(lián)系人的內(nèi)容。
[java]?view plaincopy
Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????????????????null,?null,?null,?null);??
根據(jù)Android文檔,
public final?Cursor?query?(Uri?uri,?String[]?projection,String?selection,String[]?selectionArgs,?StringsortOrder)
第一個參數(shù),uri,rui是什么呢?好吧,上面我們提到了Android提供內(nèi)容的叫Provider,那么在Android中怎么區(qū)分各個Provider?有提供聯(lián)系人的,有提供圖片的等等。所以就需要有一個唯一的標(biāo)識來標(biāo)識這個Provider,Uri就是這個標(biāo)識,android.provider.ContactsContract.Contacts.CONTENT_URI就是提供聯(lián)系人的內(nèi)容提供者,可惜這個內(nèi)容提供者提供的數(shù)據(jù)很少。
第二個參數(shù),projection,真不知道為什么要用這個單詞,這個參數(shù)告訴Provider要返回的內(nèi)容(列Column),比如Contacts Provider提供了聯(lián)系人的ID和聯(lián)系人的NAME等內(nèi)容,如果我們只需要NAME,那么我們就應(yīng)該使用:
[java]?view plaincopy
Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????new?String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME},?null,?null,?null);??
當(dāng)然,下面打印的你就只能顯示NAME了,因為你返回的結(jié)果不包含ID。用null表示返回Provider的所有內(nèi)容(列Column)。
第三個參數(shù),selection,設(shè)置條件,相當(dāng)于SQL語句中的where。null表示不進(jìn)行篩選。如果我們只想返回名稱為張三的數(shù)據(jù),第三個參數(shù)應(yīng)該設(shè)置為:
[java]?view plaincopy
Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????new?String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME},?? ????android.provider.ContactsContract.Contacts.DISPLAY_NAME?+?"='張三'",?null,?null);??
結(jié)果:
[java]?view plaincopy
11-05?15:30:32.188:?I/System.out(10271):?張三??
第四個參數(shù),selectionArgs,這個參數(shù)是要配合第三個參數(shù)使用的,如果你在第三個參數(shù)里面有?,那么你在selectionArgs寫的數(shù)據(jù)就會替換掉?,
[java]?view plaincopy
Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????new?String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME},?? ????android.provider.ContactsContract.Contacts.DISPLAY_NAME?+?"=?",?? ????????????????new?String[]{"張三"},?null);??
效果和上面一句的效果一樣。
第五個參數(shù),sortOrder,按照什么進(jìn)行排序,相當(dāng)于SQL語句中的Order by。如果想要結(jié)果按照ID的降序排列:
[java]?view plaincopy
Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????????????????null,?null,null,?android.provider.ContactsContract.Contacts._ID?+?"?DESC");??
結(jié)果:
[java]?view plaincopy
11-05?16:00:32.808:?I/System.out(12523):?31?? 11-05?16:00:32.808:?I/System.out(12523):?李四?? 11-05?16:00:32.817:?I/System.out(12523):?13?? 11-05?16:00:32.817:?I/System.out(12523):?張三??
升序,其實(shí)默認(rèn)排序是升序,+" ASC"寫不寫效果都一樣:
[java]?view plaincopy
Cursor?cursor?=?contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,?? ????????????????null,?null,null,?android.provider.ContactsContract.Contacts._ID?+?"?ASC");??
結(jié)果:
[java]?view plaincopy
11-05?15:59:10.327:?I/System.out(12406):?13?? 11-05?15:59:10.327:?I/System.out(12406):?張三?? 11-05?15:59:10.327:?I/System.out(12406):?31?? 11-05?15:59:10.327:?I/System.out(12406):?李四??
轉(zhuǎn):http://blog.csdn.net/wssiqi/article/details/8132603
總結(jié)
以上是生活随笔為你收集整理的Android之学习笔记 Contacts (一)ContentResolver query 参数详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。