【问题标题】:Reading contact phone numbers works for some and not others?阅读联系电话号码适用于某些人而不适用于其他人?
【发布时间】:2014-12-30 21:40:52
【问题描述】:

我正在从 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;
}

【问题讨论】:

    标签: android android-contacts phone-number


    【解决方案1】:

    你做错了几件事:

    1) 第二个查询必须针对 Data 表,而不是 Phone 表:

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

    并在 where 子句的 DATA1 列中指定您想要的电话号码:

    Data.MIMETYPE = Phone.CONTENT_ITEM_TYPE
    

    2) 您需要通过 RawContact 的 _ID 过滤结果

    这会将 Contact 行的 _ID 与 Phone 行的 _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);
    

    【讨论】:

    • 很高兴我能帮上忙。事实上,我认为另一个答案也有效,并且只使用 Contacts 表,所以它更简单。联系人组织在 3 个表中:联系人、原始联系人和数据。每个表都有一个对应的类,例如ContactsContract.Contacts。然后,您需要使用正确的键连接结果,就像在 SQL 查询中所做的那样。确保您阅读了有关 developer.android.com/training/contacts-provider/index.html 的所有文档。
    【解决方案2】:

    看我的回答:

    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();
        }
      }
     }
    }
    

    【讨论】:

    • 感谢您提供示例,但我真的对学习而不是破译很感兴趣。您能否解释一下您在做什么以及为什么要这样做而不是另一种方式?
    • 它多次给我相同的联系人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2014-10-12
    • 2015-02-18
    相关资源
    最近更新 更多