我在模拟器中设置了 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());