On Nov 13, 2009, at 3:33 AM, Jeff Sharkey wrote:

> Could you paste the exact query() that is returning empty for you?
> 
> Since all data is now stored in a single table you should be able to
> query on Data.CONTENT_URI and filter by Data.MIMETYPE to match the two
> types you're looking for.

I honestly get the feeling that the new 2.0 contacts API could use better 
documentation.  I've had my own little issues to sort out, and a lot of my 
working with the ContactsContract API seems to be writing tiny test programs to 
figure out 'okay, is THIS how this works...?  No...?  Hrm... okay, how about... 
no, well, that broke everything.  Let's rebuild the emulator...'

However, to share with the original poster, here's my best in-the-mail-client 
attempt at a quick bit of 'let's read all the phone numbers and emails for each 
contact.'  (I'm sure if I mucked this up, someone else can post a corrected 
one.)

----
        // Let's walk our address book.  I apologize for any typos, since this 
was
        // written in an e-mail client, rather than an editor!  Hopefully I did
        // get this right...
        //
        Cursor cursor = 
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                                                    null, null, null, null);
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor.getColumnIndex(
                                            ContactsContract.Contacts._ID));
            String hasPhone = cursor.getString(cursor.getColumnIndex(
                                            
ContactsContract.Contacts.HAS_PHONE_NUMBER));
            
            if (Boolean.parseBoolean(hasPhone)) {
                // We have a phone number, so let's query this:
                
                Cursor phones = getContentResolver().query(
                          ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                          null, 
                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" 
= "+ contactId, 
                          null, null);
                
                while (phones.moveToNext()) {
                    // Get the phone number.  If your contact has combined 
several 
                    // raw contacts, you might get a duplicated number. (I 
think?)
                    //
                    String phoneNumber = phones.getString(
                                            phones.getColumnIndex(
                                                    
ContactsContract.CommonDataKinds.Phone.NUMBER));

                    [ ... do something with phone number here ... ]             
                        
                }
                phones.close();
            }
                
            Cursor emails = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + 
contactId,
                    null, null);

            while (emails.moveToNext()) {
                // Like phone numbers, if your contact has combined several
                // raw contacts, you can get the same e-mail address more than
                // once.
                //          
                String emailAddress = emails.getString(
                                        emails.getColumnIndex(
                                                
ContactsContract.CommonDataKinds.CommonDataColumns.DATA));
                
                [ ... do something with the address ... ]
            }
            emails.close();
        }
        cursor.close();

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to