安卓;我只有 2 个联系人,但查询却可以得到 5 个,为什么?
我在模拟器中设置了 2 个测试触点。
我正在运行以下查询,它应该将它们都挑选出来,填充我的域对象,然后添加到列表中。因此底部的输出应该是2,但是却是5,这是为什么呢? (cursor.getCount() 是 5 而不是 2)
我已经完成了 while 循环的每次迭代,它多次检索相同的联系人,但 POSTCODE
的值不同,例如电话数字
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, null, null);
List<MeCercanaContact> contacts = new ArrayList<MeCercanaContact>();
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
MyContact myContact = new MyContact();
String givenName = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String postcode = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
myContact.setFirstName(givenName);
myContact.setLastName(postcode);
contacts.add(myContact);
}
}
System.out.println(contacts.size());
I have setup 2 test contacts in my emulator.
I'm running the following query, it should pick them both out, populate my domain object, and add to a list. The output at the bottom should therefore be 2, but it is 5, why is this? (cursor.getCount() is 5 instead of 2)
I have stepped through each iteration of the while loop and it is retreving the same contact multiple times, but with different values for POSTCODE
, such as the phone number
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, null, null);
List<MeCercanaContact> contacts = new ArrayList<MeCercanaContact>();
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
MyContact myContact = new MyContact();
String givenName = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String postcode = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
myContact.setFirstName(givenName);
myContact.setLastName(postcode);
contacts.add(myContact);
}
}
System.out.println(contacts.size());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 API 21 之后,我们编写此查询以删除重复的联系人。
After API 21 We Write this Query for remove contact duplicacy.
您正在查询 ContactsContract.Data,它是一个通用容器,其中包含各种联系人详细信息的列表,例如电话号码、邮政编码等。您必须筛选 ContactsContract.Data.MIMETYPE 列等于 StructuredPostal.CONTENT_ITEM_TYPE 的行的结果:
因此将查询更改为:
参见 ContactsContract.Data
You are querying ContactsContract.Data, which is a generic container that holds a list of various contact details, such as phone numbers, postal codes etc.. You must filter the results for the rows whose ContactsContract.Data.MIMETYPE column equals StructuredPostal.CONTENT_ITEM_TYPE:
So change the query to:
See ContactsContract.Data
注册到多个群组的联系人将多次显示
如果您查询
Uri CONTENT_URI = ContactsContract.Data.CONTENT_URI
将其添加到您的选择中:
a contact that is registered to multiple groups will show up multiple times
if you query the
Uri CONTENT_URI = ContactsContract.Data.CONTENT_URI
Add this to your SELECTION: