2014-11-04 62 views
1

我正在閱讀Android中聯繫人列表中的聯繫人姓名和電話號碼。我正在讀取名稱。但是,當我按姓名查找聯繫人電話號碼(在這種情況下,這些都是唯一的,這只是一個測試),它只適用於一個聯繫人,沒有爲他人獲取電話號碼,並獲得錯誤的電話號碼一個。閱讀聯繫人電話號碼適用於某些人而不是其他人?

這裏是我的getNumbers方法的代碼:

private String getNumber(String name){ 
    String returnMe=""; 
    ContentResolver contentResolver = getContentResolver(); 
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, 
      "DISPLAY_NAME = '" + getIntent().getStringExtra("name") + "'", null, null); 

    if(cursor.moveToFirst()){ 
     String identifier = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
     Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier, null, null); 
     while(phones.moveToNext()){ 
      returnMe = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
      switch(type){ 
       case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 
        System.out.println("mobile number found"); break; 
       default: 
        System.out.println("nothing found."); break; 
      } 
     } 
    } 
    return returnMe; 
} 

回答

2

你正在做的幾件事情錯了:

1)第二查詢必須目標Data表,而不是手機表:

Cursor phones = contentResolver.query(ContactsContract.Data.CONTENT_URI, ... 

,並指定要在DATA1列中的電話號碼在where子句:

Data.MIMETYPE = Phone.CONTENT_ITEM_TYPE 

2)需要由RawContact的_ID

過濾結果與此相比聯繫人的_ID隨着手機行的_ID,其中很少有機會排是相同的:

ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier 

這個Data.CONTACT_ID與光標的Contact._ID屬性比較

Data.CONTACT_ID + " = " + identifier 

Data的javadoc頁面上的例子給出了一個較爲完整的例子:

查找特定類型的所有數據對於一個給定的接觸

Cursor c = getContentResolver().query(Data.CONTENT_URI, 
     new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, 
     Data.CONTACT_ID + "=?" + " AND " 
       + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", 
     new String[] {String.valueOf(contactId)}, null); 
+0

很高興我能幫到你。事實上,我認爲其他答案也適用,只使用聯繫人表,因此更簡單。聯繫人分爲3個表格:聯繫人,RawContacts和數據。每個表具有相應的類,例如, ContactsContract.Contacts。然後,您需要使用正確的鍵連接結果,就像您在SQL查詢中所做的那樣。確保你閱讀了關於所有https://developer.android.com/training/contacts-provider/index.html的文檔。 – 2014-11-11 05:12:45

0

看到我的回答是:

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    if (cur.getCount() > 0) { 
    while (cur.moveToNext()) { 
     String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
     //Query phone here 
     if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
     Cursor pCur = cr.query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
     null, 
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
     new String[]{id}, null); 
     while (pCur.moveToNext()) { 
     // Get phone numbers here 
     } 
     pCur.close(); 
    } 
    } 
} 
} 
+0

謝謝你的樣品,但我對於學習而不是破譯真的很感興趣。你能否給我一個解釋,說明你在做什麼,爲什麼你這樣做,而不是另一個? – CoffeeSaurus 2014-11-11 05:01:45

+0

它給了我多次相同的聯繫人。 – Shivang 2016-05-02 06:52:56

相關問題