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

发布于 2024-10-24 21:56:24 字数 535 浏览 2 评论 0原文

有谁知道如何获取属于 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);
}

Does anybody know how to get a list of contacts belonging to a 1 specific group in Android?

I need something like this:

Select * from contacts where group_id = "1234"

I am able to get a list of all contacts OR all groups by using something like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

默嘫て 2024-10-31 21:56:25

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

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

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

this is what i use and it works fine for me

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

this requires you to have the Group id already and that can be found by querying ContactsContract.Groups

楠木可依 2024-10-31 21:56:25
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 找到联系人。

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

Hope this will help you to find contact According to group id.

分开我的手 2024-10-31 21:56:25
    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();
    }
}

并从你想要的地方调用这个函数

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

and call this function from where you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文