2015-02-05 47 views
1
Cursor cursor = contentResolver.query(Data.CONTENT_URI, null, "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE+ "=?) AND " + Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts)", new String[] {Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE }, Data.CONTACT_ID); 

我想按字母順序提取我的聯繫人,但是此查詢以非字母順序提取它們。我不知道在這個查詢附加asc順序。請幫助在android中按字母順序獲取聯繫人?

回答

2

第五個參數是sortOrder。你正在通過Data.CONTACT_ID

Cursor cursor = contentResolver.query(Data.CONTENT_URI, null, "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE+ "=?) AND " + Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts)", new String[] {Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE }, Data.DISPLAY_NAME+" ASC"); 
+0

感謝您的答覆,其工作正常,但其採取更多的時間比我上次查詢 –

+0

不客氣。如果您覺得我的答案幫助您前進並回答了您的問題,請您將其標記爲「已接受」?謝謝! –

0

| * |在查詢的5個參數中添加下面的行,因爲它是針對排序順序的。

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC" 

| * |優化:按字母順序顯示得姓名和號碼與標籤和照片縮略圖在一個查詢:

ContentResolver contentResolver = getContentResolver(); 
Cursor contctKsrVar = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      new String[] { 
        ContactsContract.CommonDataKinds.Phone._ID, 
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
        ContactsContract.CommonDataKinds.Phone.NUMBER, 
        ContactsContract.CommonDataKinds.Phone.TYPE, 
        ContactsContract.CommonDataKinds.Phone.LABEL, 
        ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI}, 
      null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
相關問題