Android 联系人查询出现重复项
我正在尝试获取某个联系人的所有电话号码。当我使用光标查询与联系人关联的号码时,我得到每个号码的重复项。经过一番窥探后,我相信这是由于链接的个人资料(即 Google 个人资料和电话联系人个人资料)造成的。这是我提取数字的代码:
Cursor cursor = getContentResolver().query(
Phone.CONTENT_URI,
new String[]{PhoneLookup.NUMBER},
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
while(cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(), phoneNumber, Toast.LENGTH_LONG).show();
}
cursor.close();
有没有办法将此查询限制到某个配置文件? 提前致谢。我已经对此进行了一段时间的搜索,但无法找到任何解决方案。
I'm trying to get all of the phone numbers for a contact. When I query the numbers associated with a contact with a cursor, I get duplicates of every number. After snooping around I believe that this is because of the Linked Profiles (i.e. Google profile and Phone Contacts profile). Here is my code for pulling out the numbers:
Cursor cursor = getContentResolver().query(
Phone.CONTENT_URI,
new String[]{PhoneLookup.NUMBER},
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
while(cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(), phoneNumber, Toast.LENGTH_LONG).show();
}
cursor.close();
Is there a way to limit this query to a certain profile?
Thanks in advance. I've searched on this for awhile and been unable to find any solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近遇到了类似的问题(例如,缺少 group by 子句),我设法通过将 ContentProvider 中的数据插入临时表并查询表中的结果来解决此问题。
故事是 ContentProvider 背后的数据可能不是数据库。它可以是 XML、JSON、文件系统等...因此这些没有 Group By 选项,因此他们将其排除在外。您也不能假设 count(_id) 总是有效。
I was struggling with similar stuff lately (eg. missing the group by clause), and I managed to workaround by inserting the data from ContentProvider into a temp table and querying the table for results.
The story is that the data behind a ContentProvider might not be a database. It can be XML, JSON, FileSystem etc... So these do not have the Group By option, and therefor they left it out. You also can't suppose always that count(_id) will work.