7

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?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
pankajagarwal
  • 13,462
  • 14
  • 54
  • 65
  • @Octavian: I'm not sure I agree with removing *Android* from the question title. This seems to be a pretty common occurrence for questions on other technologies. This may be an editing question for meta – NotMe Feb 05 '11 at 23:41
  • @ChrisLively I have to admit that you might be right on this one. I will change it back. – Octavian Helm Feb 06 '11 at 12:04

1 Answers1

12

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
  • 90,123
  • 14
  • 117
  • 115
  • 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 – pankajagarwal Feb 05 '10 at 08:58
  • @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. – Mark B Feb 05 '10 at 14:59
  • 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 Feb 05 '10 at 15:19
  • 4
    i had problem with projection `ContactsContract.CommonDataKinds.Phone ContactsContract.CommonDataKinds.Email` android 2.2 – Trung Nguyen May 31 '12 at 10:47