安卓;我只有 2 个联系人,但查询却可以得到 5 个,为什么?

发布于 2024-09-16 18:58:21 字数 1140 浏览 4 评论 0原文

我在模拟器中设置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瑶 2024-09-23 18:58:21

在 API 21 之后,我们编写此查询以删除重复的联系人。

String select = ContactsContract.Data.HAS_PHONE_NUMBER + " != 0 AND " + 
ContactsContract.Data.MIMETYPE
                + " = " + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + " 
AND "+ ContactsContract.Data.RAW_CONTACT_ID + " = " + 
ContactsContract.Data.NAME_RAW_CONTACT_ID;

Cursor cursor = mContent.query(ContactsContract.Data.CONTENT_URI, null, select, 
 null, null);

After API 21 We Write this Query for remove contact duplicacy.

String select = ContactsContract.Data.HAS_PHONE_NUMBER + " != 0 AND " + 
ContactsContract.Data.MIMETYPE
                + " = " + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + " 
AND "+ ContactsContract.Data.RAW_CONTACT_ID + " = " + 
ContactsContract.Data.NAME_RAW_CONTACT_ID;

Cursor cursor = mContent.query(ContactsContract.Data.CONTENT_URI, null, select, 
 null, null);
独闯女儿国 2024-09-23 18:58:21

您正在查询 ContactsContract.Data,它是一个通用容器,其中包含各种联系人详细信息的列表,例如电话号码、邮政编码等。您必须筛选 ContactsContract.Data.MIMETYPE 列等于 StructuredPostal.CONTENT_ITEM_TYPE 的行的结果:

因此将查询更改为:

Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
     null, null, ContacsContract.Data.MIMETYPE +  "='" + 
ContactsContract.StructuredPostal.CONTENT_ITEM_TYPE + "'", null);

参见 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:

Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
     null, null, ContacsContract.Data.MIMETYPE +  "='" + 
ContactsContract.StructuredPostal.CONTENT_ITEM_TYPE + "'", null);

See ContactsContract.Data

樱桃奶球 2024-09-23 18:58:21

注册到多个群组的联系人将多次显示
如果您查询 Uri CONTENT_URI = ContactsContract.Data.CONTENT_URI

将其添加到您的选择中:

 + ContactsContract.Data.DATA1 + " = 1 " ;  //show only contacts in group 1

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:

 + ContactsContract.Data.DATA1 + " = 1 " ;  //show only contacts in group 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文