你如何获得一个联络小组的成员? [英] How do you get the members of a contact group?

查看:121
本文介绍了你如何获得一个联络小组的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个联络小组的ID,我想列出其成员。这里的code我想:

I have the ID of a contact group, and I'd like to list its members. Here's the code I'm trying:

String[] projection = new String[]{
    ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID 
};
Cursor contacts = getContentResolver().query(
        Data.CONTENT_URI,
        projection,
        ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + gid,
        null,
        null
);
String result = "";
do {
    result += contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID)) + " ";
} while (contacts.moveToNext());

但是,这将引发异常:

But this throws an Exception:

03-24 17:11:33.097: ERROR/AndroidRuntime(10730): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
...
03-24 17:11:33.097: ERROR/AndroidRuntime(10730):     at myapp.MultiSend$1.onItemClick(MultiSend.java:83)

这是开始行结果+ = 。谁能告诉我,我做错了什么,或提出一个更好的方式来获得相同的信息?

which is the line starting result +=. Can anyone tell me what I'm doing wrong, or suggest a better way to get the same information?

推荐答案

试试这个code片段,希望它有助于

Try this code snippet, hope it helps

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

Cursor contacts = getContentResolver().query(
        Data.CONTENT_URI,
        projection,
        ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + gid,
        null,
        null
);

startManagingCursor(contacts);

String result = "";

if (contacts.moveToFirst()) {

      do {

            try {

    result += contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID)) + " ";


                } catch (Exception ex) {
                                        ex.printStackTrace();
                                       }
} while (contacts.moveToNext());

}

这篇关于你如何获得一个联络小组的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆