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?
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? 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
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With