如何使用游标获取android中给定联系人组的联系人成员的电话号码

时间:2016-08-26 09:10:34

标签: android android-contacts android-cursor

我从光标中获取了组ID。让我们说groupID为' 59'。我想从Group_ID为59的组中获取所有联系人。我已经使用不同类的ContactsContract操纵了投影,选择和光标,但没有用。以下是我获取成员联系人的代码。

Uri uri = ContactsContract.Data.CONTENT_URI;

                    String[] projection22 = new String[] {
                            ContactsContract.Contacts._ID,
                            ContactsContract.Data.CONTACT_ID,
                            ContactsContract.Data.DISPLAY_NAME
                    };

                    String selection = null;
                    String[] selectionArgs = null;

                    if(groupID != null && !"".equals(groupID)) {
                        selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
                                + " = ?";
                        selectionArgs = new String[] { groupID };
                    }

                    String sortOrder = null;

                    Cursor cursor = getContentResolver().query(uri, projection22,
                            selection, selectionArgs, sortOrder);

现在光标返回零计数。请让我以正确的方式使用光标。

1 个答案:

答案 0 :(得分:0)

    public void getSampleContactList(int groupID) {

    contactList = new ArrayList<ConatctData>();
    Uri groupURI = ContactsContract.Data.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID };

    Cursor c = getContentResolver().query(
            groupURI,
            projection,
            ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
                    + "=" + groupID, null, null);

    while (c.moveToNext()) {
        String id = c
                .getString(c
                        .getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
        Cursor pCur = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] { id }, null);

        while (pCur.moveToNext()) {
            ConatctData data = new ConatctData();
            data.name = pCur
                    .getString(pCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            data.phone = pCur
                    .getString(pCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contactList.add(data);
        }

        pCur.close();

    }
}

class ConatctData {
    String phone, name;
}

original post