如何获得联系人组的成员?

发布于 2024-10-27 05:23:41 字数 915 浏览 1 评论 0原文

我有联系人组的 ID,我想列出其成员。这是我正在尝试的代码:

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

但这会引发异常:

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)

这是以 result += 开头的行。谁能告诉我我做错了什么,或者建议更好的方法来获取相同的信息?

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?

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

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

发布评论

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

评论(2

你的呼吸 2024-11-03 05:23:41

试试这个代码片段

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

Try this code snippet

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());
}
有木有妳兜一样 2024-11-03 05:23:41

当列不存在时,Cursor.getColumnIndex(String column) 返回 -1,这会导致 Cursor.getString(int colidx) 引发异常。

我将通过为查询调用的第三个参数传递 null 来开始测试,以查看是否从调用中获得有效的游标。

如果您没有获得有效的 Cursor,那么我会检查以确保 Data.CONTENT_URI 是要调用的正确 CONTENT_URI。如果没有看到您的导入,很难判断完全限定的路径是什么。 (编辑:看起来 ContactsContract.Data.CONTENT_URI 必须是那里的常量。)

如果您确实获得了有效的 Cursor,则第三个参数可能存在问题。

Cursor.getColumnIndex(String column) returns -1 when the column does not exist, and that is causing Cursor.getString(int colidx) to throw the exception.

I'd start testing by passing null for the third argument of the query call to see if you get a valid Cursor from the call.

If you don't get a valid Cursor, then I'd check to make sure that Data.CONTENT_URI is the right CONTENT_URI to call. It's hard to tell what the fully qualified path is without seeing your imports. (Edit: It looks like ContactsContract.Data.CONTENT_URI must be the constant there.)

If you do get a valid Cursor, then there might be an issue with that third argument.

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