1.cursor query 方法入?yún)?/p>
public final Cursor query (Uri uri, String[] projection,String selection,String[] selectionArgs, String sortOrder)
第一個參數(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)該使用:
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è)置為:
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é)果:
11-05 15:30:32.188: I/System.out(10271): 張三
第四個參數(shù)胁住,selectionArgs趁猴,這個參數(shù)是要配合第三個參數(shù)使用的,如果你在第三個參數(shù)里面有彪见?儡司,那么你在selectionArgs寫的數(shù)據(jù)就會替換掉?余指,
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的降序排列: