gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:42:35 25 4
gpt4 key购买 nike

我有一个联系人组的 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 += 开始的行。任何人都可以告诉我我做错了什么,或者建议一种更好的方法来获取相同的信息吗?

最佳答案

试试这个代码 fragment

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

关于android - 您如何获得联系人组的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com