Question

I'm trying to go over all contacts using the contacts, raw_contacts and data tables.

From reading online I believe that Contacts._id is connected with RawContacts.contact_id and Data.raw_contact_id is connected with RowContacts._id. Am I right?

Believing in this I built this method:

public void testingContactsDatabase(String contactId)
    {
        final String[] projection = new String[] {
                Contacts._ID,
                Contacts.DISPLAY_NAME,
                Data.RAW_CONTACT_ID,
                Data.MIMETYPE,
                StructuredName.DISPLAY_NAME, 
                StructuredName.FAMILY_NAME,
                StructuredName.GIVEN_NAME, 
                StructuredName.MIDDLE_NAME,
                StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
                StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
                };

        final String selection = Contacts._ID + " = " + contactId + " AND " 
                    + Contacts._ID + " = " + RawContacts.CONTACT_ID + " AND "
                    + Data.RAW_CONTACT_ID + " = " + RawContacts._ID;

        final Cursor curStructuredName = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI, 
                projection, 
                selection, null,
                //new String[] {contactId, ContactsContract.RawContacts.CONTACT_ID, RawContacts._ID},
                null
                );

        if(curStructuredName.moveToFirst())
        {
            Log.d("XYZ", "Found something");
        }
    }

But it never finds anything. I'm not sure if I'm using the correct URI, I've tried with different URIs and either got exceptions or same ouput.

Can someone point me out where I'm doing something wrong?

Thanks

Was it helpful?

Solution

I've solved my problem. Here is my code working now:

// Get the structured name fields
    public ArrayList<String> getStructuredName(String contactId)
    {
        ArrayList<String> structuredList = new ArrayList<String>();

        final String[] projection = new String[] {
                StructuredName.DISPLAY_NAME, 
                StructuredName.FAMILY_NAME,
                StructuredName.GIVEN_NAME, 
                StructuredName.MIDDLE_NAME,
                StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
                StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
        };

        final String filter = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?"; 
        final String parameters[] = {StructuredName.CONTENT_ITEM_TYPE, contactId};

        Cursor cursor =  context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
                projection, filter, parameters, null);

        if(cursor.moveToFirst())
        {
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            {
                final String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
                final String familyName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
                final String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
                final String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
                final String prefix = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));
                final String suffix = cursor.getString(cursor.getColumnIndex(StructuredName.SUFFIX));

                structuredList.add(displayName);
                structuredList.add(familyName);
                structuredList.add(givenName);
                structuredList.add(middleName);
                structuredList.add(prefix);
                structuredList.add(suffix);

                Log.d(tag, "/////////////////////////////////////////////////////");
                Log.d(tag, "displayName: " + displayName );
                Log.d(tag, "familyName: " + familyName );
                Log.d(tag, "givenName: " + givenName );
                Log.d(tag, "middleName: " + middleName );
                Log.d(tag, "prefix: " + prefix );
                Log.d(tag, "suffix: " + suffix );
                Log.d(tag, "/////////////////////////////////////////////////////");

            }
        }

        cursor.close();
        return structuredList;

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top