2015-10-02 135 views
0

我試圖獲取與android上給定電話號碼關聯的所有聯繫人姓名。如何使用電話號碼獲取所有聯繫人姓名?

public void getContactName(Context context, String phoneNumber) { 
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    Cursor cursor = context.getContentResolver().query(uri, new String[]{ 
     ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null); 

    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID)); 
      System.out.println("ID : " + contactId + " Name : " + contactName); 
     } 
     cursor.close(); 
    } 
} 

但我真的得到重複值

10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac 
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac 
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3401 Name : Ann 
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 2819 Name : Sam 
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac 
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac 
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 2819 Name : Sam 

這有什麼錯的代碼?如何通過傳遞電話號碼獲取所有聯繫人姓名?

+0

加上'TYPE'和'NUMBER'你的投影和使用'DatabaseUtils.dumpCursor' – pskink

+0

@pskink'光標光標= context.getContentResolver()查詢(URI,新String [] {ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.PhoneLookup ._ID,ContactsContract.PhoneLookup.TYPE,ContactsContract.PhoneLookup.NUMBER},null,null,null); DatabaseUtils.dumpCursor(cursor);'這是正確的嗎?我得到了相同的結果 – Selvam

+0

我沒有看到logcat – pskink

回答

0

我真的不`噸知道爲什麼光標返回值相同的多個times.To獲得獨特的價值觀,我存儲在其中的ArrayList通過循環聯絡人ID只擁有獨特values.Then我可以得到的聯繫方式與IDS

public void getContactName(Context context, String phoneNumber) { 
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    Cursor cursor = context.getContentResolver().query(uri, 
    new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID},null, null, null); 
    List<String> listIds = new ArrayList<String>(); //Arraylist to hold the unique ids 
    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID)); 
      if(!listIds.contains(contactId)) 
       listIds.add(contactId); //adding unique id to arraylist 
     } 
     //pass unique ids to get contact names 
     for (int i = 0; i < listIds.size(); i++) { 
      String newId = listIds.get(i); 
      Cursor cursorDetails = context.getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
        new String[]{newId}, null); 
      if (cursorDetails != null) { 
       if (cursorDetails.moveToFirst()) { 
        String contactName = cursorDetails.getString(cursorDetails.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
        System.out.println("ID : " + newId + " Name : " + contactName + " Number : " + phoneNumber); 
       } 
       cursorDetails.close(); 
      } 
     } 
     cursor.close(); 
    } 

} 

logcat的輸出:

10-03 12:24:08.161 6306-6306/? I/System.out: ID : 3480 Name : Issac 
10-03 12:24:08.161 6306-6306/? I/System.out: ID : 3401 Name : Ann 
10-03 12:24:08.171 6306-6306/? I/System.out: ID : 2819 Name : Sam 
相關問題