tags:

views:

69

answers:

1

My Android app needs to retrieve information about a contact (namely, phone number and name.) I wrote some code using getContentResolver().query() In the results, I am seeing that the name is being returned, and other information like when I last contacted that person, but not the phone number. What am I doing wrong?

The code:

Log.w("MyApp", "requesting " + phonesUri.toString());
Cursor contactCursor = context.getContentResolver().query(person, null, null, null, null);
contactCursor.moveToFirst();

for (int i = 0; i < contactCursor.getColumnCount(); i ++) {
   Log.w("MyApp", contactCursor.getColumnName(i) + ": " + contactCursor.getString(i));
}

which outputs this:

W/MyApp (21477): requesting content://contacts/people/169/phones
W/MyApp (21477): times_contacted: 6
W/MyApp (21477): custom_ringtone: null
W/MyApp (21477): primary_organization: null
W/MyApp (21477): phonetic_name: 
W/MyApp (21477): status: null
W/MyApp (21477): label: null
W/MyApp (21477): number: null
W/MyApp (21477): type: null
W/MyApp (21477): mode: null
W/MyApp (21477): last_time_contacted: 1278113641980
W/MyApp (21477): display_name: (name removed for privacy)
W/MyApp (21477): im_handle: null
W/MyApp (21477): _id: 169
W/MyApp (21477): number_key: null
W/MyApp (21477): starred: 0
W/MyApp (21477): primary_email: null
W/MyApp (21477): name: (name removed for privacy)
W/MyApp (21477): primary_phone: null
W/MyApp (21477): im_account: null
W/MyApp (21477): notes: 
W/MyApp (21477): im_protocol: null
W/MyApp (21477): send_to_voicemail: 0
+3  A: 
  1. You don't need to write your own iterate method to print to screen/log, call DatabaseUtils.dumpCursorToString(cursor); on a cursor to get a String representation of the raw output.

  2. Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

        if (Integer.parseInt(cur.getString(
               cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);
        while (pCur.moveToNext()) {
        // Do something with phones
        } 
        pCur.close();
    }
    

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

Check out this tutorial Working With Android Contacts

Pentium10
Thank you. It seems that the old pre-Eclair stuff does not work reliably anymore. Once I switched to ContactsContract, things started working.
Josh K