views:

679

answers:

2

Given a contact id, I can obtain various contact details (like name, phone, email-id, etc) by making different queries for a each of these fields.

But is there a method to obtain all the details associated with this contact id by making a single query

+4  A: 

Had to change a bit of the tutorial on Content Providers since it referenced deprecated classes, this might help.

import android.provider.ContactsContract.Contacts;
import android.database.Cursor;

// Form an array specifying which columns to return, you can add more.
String[] projection = new String[] {
                         ContactsContract.Contacts.DISPLAY_NAME,
                         ContactsContract.CommonDataKinds.Phone
                         ContactsContract.CommonDataKinds.Email
                      };

Uri contacts =  ContactsContract.Contacts.CONTENT_LOOKUP_URI;
// id of the Contact to return.
long id = 3;

// Make the query. 
Cursor managedCursor = managedQuery(contacts,
                     projection, // Which columns to return 
                     null,       // Which rows to return (all rows)
                                 // Selection arguments (with a given ID)
                     ContactsContract.Contacts._ID = "id", 
                                 // Put the results in ascending order by name
                     ContactsContract.Contacts.DISPLAY_NAME + " ASC");
Anthony Forloney
Thanks for the prompt reply. But I'm using 1.6 SDK and ContactsContract seems to works for 2.0 and above. Any solution for 1.6 and below versions of Android
frieza
@frieza Go back and look at that Content Providers tutorial linked to in the top of the answer. Apparently Anthony took the tutorial and updated it to 2.0 when he posted his answer, but what you need is the original tutorial.
mbaird
Er, that was my fault I went ahead and assumed it was the 2.0 SDK, but mbaird is right, the tutorial in the `Content Providers` link should work just fine for you.
Anthony Forloney
A: 

The code snippet you have provided does not work ...

Since you have written

Uri contacts = ContactsContract.Contacts.CONTENT_LOOKUP_URI;

Therefore it queries only "ContactsContract.Contacts.DISPLAY_NAME" because it is in Contacts Package

But if you want Email and Phone, which are in CommanDataKinds package you have to do Uri contacts = ContactsContract.CommonDataKinds.Email.CONTENT_URI; for retrieviong Email

and / or

Uri contacts ContactsContract.CommonDataKinds.Phone.CONTENT_URI; for retrieving Phone ...

I am still having troubles getting other informations such as Company name , Title, notes to retrieve from Phonebook...

Shahab

Shahab