获取属于特定组的联系人列表

时间:2011-03-23 09:51:25

标签: android list contacts android-contacts

有人知道如何获取属于Android中1个特定群组的联系人列表吗?

我需要这样的东西:

Select * from contacts where group_id = "1234"

我可以使用以下内容获取所有联系人或所有群组的列表:

Cursor groupCursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
final ArrayList<String> contacts = new ArrayList<String>();

while(groupCursor.moveToNext()) {
    String name = groupCursor.getString(groupCursor.getColumnIndex(ContactsContract.Constacts.DisplayName    ));
    contacts.add(name);
}

3 个答案:

答案 0 :(得分:7)

这是我使用的,它对我来说很好

Uri groupURI = ContactsContract.Data.CONTENT_URI;


    String[] projection = new String[]{
     ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID ,
     ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};

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

这要求您已经拥有Group ID,并且可以通过查询ContactsContract.Groups找到它

答案 1 :(得分:6)

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

希望这可以帮助您根据群组ID找到联系方式。

答案 2 :(得分:1)

    void getlistofcontacts( long groupId ) {
    String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID };

    Cursor groupCursor = getContentResolver().query(
            Data.CONTENT_URI,
            cProjection,
            CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
                    + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
                    + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
            new String[] { String.valueOf(groupId) }, null);
    if (groupCursor != null && groupCursor.moveToFirst())
    {
        //Toast.makeText(this,"if",1000).show();
        do
        {

            int nameCoumnIndex = groupCursor.getColumnIndex(Phone.DISPLAY_NAME);

            String name = groupCursor.getString(nameCoumnIndex);

            long contactId = groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID));

            Cursor numberCursor = getContentResolver().query(Phone.CONTENT_URI,
                    new String[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null);

            if (numberCursor.moveToFirst())
            {
                int numberColumnIndex = numberCursor.getColumnIndex(Phone.NUMBER);
                do
                {
                    String phoneNumber = numberCursor.getString(numberColumnIndex);

                    Toast.makeText(this,name+phoneNumber,1000).show();
                } while (numberCursor.moveToNext());
                numberCursor.close();
            }
            else
            {
                Toast.makeText(this,"no contact are there",1000).show();
            }
        } while (groupCursor.moveToNext());
        groupCursor.close();
    }
    else
    {
        Toast.makeText(this,"no such group exists",1000).show();
    }
}

并从您想要的地方调用此功能