1

i'm doing some tests to figure out how the provider works with events etc.

I'm trying to get all contacts of specific accounts, then read details of each contact to get the birthday if present.

I firstly tried to do it in 1 go but I didn't have enough knowledge of the provider so I'm trying to do it in 2 steps for know just to see if I can get to it.

I get Invalid column on this line: Cursor cursor = cr.query(uri, projection, [...] The contact_id is the problem here. How can I improve the second query to get the data I want? I tried various mods on the code getting nowhere.

Thanks.

The code I use:

private void readContacts(){
        Cursor contByAccCursor = getContactsByAccounts();       
        getContactsBirthdays(contByAccCursor);
    }

    private Cursor getContactsByAccounts() {
        Uri uri = ContactsContract.RawContacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.RawContacts.CONTACT_ID,
                ContactsContract.RawContacts.ACCOUNT_TYPE
        };
        String where = ContactsContract.RawContacts.ACCOUNT_TYPE + " IN ('com.google', 'vnd.sec.contact.phone', 'com.skype.contacts.sync')";
        return activity.getContentResolver().query(uri, 
                                                    projection, 
                                                    where, 
                                                    null, 
                                                    null);
    }

    private void getContactsBirthdays(Cursor filteredContacts) {
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = new String[] {
                //ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE
        };
        String where =
                ContactsContract.Data.MIMETYPE + "= ? AND " +
                ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
                ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " AND " +
                ContactsContract.CommonDataKinds.Event.CONTACT_ID + "= ?";

        String[] selectionArgs;
        String sortOrder = null;

        StringBuffer sb = new StringBuffer();

        ContentResolver cr = activity.getContentResolver();

        int accTypeCol = filteredContacts.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE);

        while(filteredContacts.moveToNext()){

            selectionArgs = new String[] { 
                    ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
                    filteredContacts.getString(filteredContacts.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID))
            };

            Cursor cursor = cr.query(uri, 
                        projection, 
                        where, 
                        selectionArgs, 
                        sortOrder);

            if(cursor.moveToNext()){                
                String dName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                String bDay = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                String accType = filteredContacts.getString(accTypeCol);

                sb.append(dName + "("+accType+")" + " - " + bDay);
                sb.append("\n");
            }
        }       
        Log.d(TAG, sb.toString());
    }
user6547359
  • 204
  • 1
  • 9

1 Answers1

0

Ok I feel so stupid now. The error is the second content_uri, I was getting the wrong one, must be tired...

This is the correct uri to get the right data:

private void getContactsBirthdays(Cursor filteredContacts) {
        Uri uri = ContactsContract.Data.CONTENT_URI;
user6547359
  • 204
  • 1
  • 9