5

我正在尝试在应用程序内的列表视图中显示来自用户电话的联系人列表。我可以获取联系人,但有些联系人会有多个手机号码,所以我想多次显示那个人。

Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    String name, number = "";
    String id;
    c.moveToFirst();
    for (int i = 0; i < c.getCount(); i++) {
        name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                    null);
            while (pCur.moveToNext()) {
                number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
        }
        Log.i("name ", name + " ");
        Log.i("number ", number + " ");
        c.moveToNext();

想要向用户显示与他拥有的号码一样多的次数。我什至可以仅根据 10 位数长的手机号码将其列入短名单吗?

Example

Name: John Doe 
Number 1: xxxxxxxxx
Number 2: xxxxxxxxx

Name: Sarah 
Number 1: xxxxxxxxx

这应该返回三个列表项,如下所示

John Doe  xxxxxxxxx
John Doe  xxxxxxxxx
Sarah     xxxxxxxxx
4

2 回答 2

2

你可以试试这样的

List<PhoneItem> phoneNoList = new ArrayList<PhoneItem();
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String name, number = "";
String id;
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

    if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                null);
        while (pCur.moveToNext()) {
            phoneNoList.add(new PhoneItem(name, pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
        }
    }
    c.moveToNext();


}

for (PhoneItem row : phoneNoList) {
    Log.i("name", row.name);
    Log.i("number", row.number+"");
}

[...]

private class PhoneItem {
   String name;
   String phone;

   public PhoneItem(String name, String phone) {
       this.name = name;
       this.phone = phone;
   }
}
于 2013-04-19T06:57:51.910 回答
1

下面的代码将获取所有带有电话号码的联系人。可能有重复,因为同一个联系人可能属于不同的组。您必须循环并消除重复项。

String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,};
String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?" ;  
String[] selectionArgs = new String[] {"1"};                            
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                    projection, 
                                    selection, 
                                    selectionArgs, 
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                                    + ", " + ContactsContract.CommonDataKinds.Phone.NUMBER);
于 2013-04-19T07:23:53.260 回答