日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之学习笔记 Contacts (一)ContentResolver query 参数详解

發(fā)布時間:2023/12/4 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之学习笔记 Contacts (一)ContentResolver query 参数详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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進行的。提供數(shù)據(jù)(內(nèi)容)的就叫Provider,Resovler提供接口對這個內(nèi)容進行解讀。

    在這里,系統(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表示不進行篩選。如果我們只想返回名稱為張三的數(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,按照什么進行排序,相當(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):?張三??
  • 升序,其實默認(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)容還不錯,歡迎將生活随笔推薦給好友。