user1513272
user1513272

Reputation: 43

Getting Contact Name Using This Implementation

I'm trying to use the implementation of the code found in this question post: How to read contacts on Android 2.0 but I can't figure out how to get it also run through the given, family, or display name columns. How can I get this implementation (the large one in the linked question) to give me the given and display names of the contacts as it loops through each row? I want to use this implementation specifically because it loops through the specified columns in each row and returns the information in the order it is in the row.

Here is the implementation from the other question that I'm referring to:\

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)) { 
      // You know it has a number so now query it like this
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 

while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
  } 
  phones.close(); 
   }

   Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 

   while (emails.moveToNext()) { 
      // This would allow you get several email addresses 
      String emailAddress = emails.getString( 
      emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

   } 

   emails.close();
}

cursor.close(); 

Upvotes: 0

Views: 138

Answers (1)

Joe Malin
Joe Malin

Reputation: 8641

First of all, the answer in the linked post is a bit obsolete, because there now is documentation for Contacts Provider at developer.android.com.

Second, the problem you're having is that you're querying the "data" table with a contact ID for the contacts table, and that won't work.

The Contacts Provider is a three-tiered arrangement of tables. The top level is the Contacts table, whose constants are defined in ContactsContract.Contacts. One of its columns is ContactsContract.Contacts._ID, which identifies a contact row. HOWEVER, a row in this table is an aggregation of individual contacts from various sources.

The individual contacts are stored in ContactsContract.RawContacts. For each ContactsContract.Contacts._ID, there can be more than one row in ContactsContract.RawContacts.

For each row in ContactsContract.RawContacts, there are one or more rows in ContactsContract.Data. Each row has a MIME type that tells you what type of data it is. For example, a row in ContactsContract.RawContacts can have three rows in ContactsContract.Data that have the MIME type for phone numbers. Each of the three "data" rows is a different type of phone number (home, mobile, work) for the contact in ContactsContract.RawContacts.

You can see why looking for ContactsContract.Contacts._ID in ContactsContract.Data won't work; that's the wrong ID to look for.

Rather than re-write the documentation here, I suggest you take a look at it. It has some nice illustrations that help explain what I'm getting at: Contacts Provider

Upvotes: 1

Related Questions