Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain all details of a contact in Android

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?

like image 999
pankajagarwal Avatar asked Feb 05 '10 05:02

pankajagarwal


People also ask

What is ContactsContract?

ContactsContract defines an extensible database of contact-related information. Contact information is stored in a three-tier data model: A row in the Data table can store any kind of personal data, such as a phone number or email addresses. The set of data kinds that can be stored in this table is open-ended.

How to read all contacts in Android?

How to read all contacts in android? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken list view. Step 3 − Add the following code to src/MainActivity.java

How do I retrieve all the details for a contact?

To retrieve all the details for a contact, search the ContactsContract.Data table for any rows that contain the contact's LOOKUP_KEY. This column is available in the ContactsContract.Data table, because the Contacts Provider makes an implicit join between the ContactsContract.Contacts table and the ContactsContract.Data table.

What is a contact list and how does it work?

It's the details that users are looking for when they retrieve a contact. You can give them all the details for a contact, or only display details of a particular type, such as email addresses.

Where can I find the lookup_key column in the contacts contract?

This column is available in the ContactsContract.Data table, because the Contacts Provider makes an implicit join between the ContactsContract.Contacts table and the ContactsContract.Data table. The LOOKUP_KEY column is described in more detail in the Retrieving contact names lesson.


1 Answers

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");
like image 81
Anthony Forloney Avatar answered Sep 20 '22 16:09

Anthony Forloney