2014-11-04 65 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

相关问题