Android; I only have 2 contacts, yet I can obtain 5 from a query, why?
Asked Answered
P

3

0

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());
Pointsman answered 27/8, 2010 at 22:37 Comment(0)
T
1

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

Trovillion answered 28/8, 2010 at 15:33 Comment(0)
M
2

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);
Maggoty answered 4/5, 2019 at 4:58 Comment(0)
T
1

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

Trovillion answered 28/8, 2010 at 15:33 Comment(0)
J
0

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
Janitajanith answered 2/8, 2017 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.