Android;我只有2个联系人,但我可以从查询获得5,为什么? [英] Android; I only have 2 contacts, yet I can obtain 5 from a query, why?
问题描述
我在我的模拟器中设置了2个测试联系人。
I have setup 2 test contacts in my emulator.
我运行以下查询,应该选择它们,填充我的域对象,到列表。因此底部的输出应该是2,但是它是5,为什么是这个? (cursor.getCount()是5而不是2)
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)
我已经走过while循环的每次迭代,并且它多次撤回同一个联系人, POSTCODE
的值,例如电话号码
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());
推荐答案
您正在查询ContactsContract.Data,容器,其中包含各种联系人详细信息(例如电话号码,邮政编码等)的列表。您必须过滤ContactsContract.Data.MIMETYPE列等于StructuredPostal.COLTENT_ITEM_TYPE的行的结果:
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:
因此将查询更改为:
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, ContacsContract.Data.MIMETYPE + "='" +
ContactsContract.StructuredPostal.CONTENT_ITEM_TYPE + "'", null);
这篇关于Android;我只有2个联系人,但我可以从查询获得5,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!