Currently I am displaying all the contacts of my phone in my app as a custom recyclerview. Till now I am showing name, mobile number and profile image of the contact on the list item view but I need to get all the information for that contact and display it when the list item of my app is clicked in a custom detail page.
For each contact there is a different set of information available, like in few contacts I have email but for few contacts its not present.
My questions are
How can I get all the information of a given contact without missing a single bit.Is there a structure that I can traverse and check value for each key?
Also when I am populating the system contacts in my apps list, I find same contact multiple times in the list. I think this is due to the fact that in my device's account manager the same number is registered for many accounts like whatsapp, gmail. If so how to display that number only once in my list.
1. For Android 7 : go to "Settings" > "About" > "Hardware information". 2. The screen of "Hardware information" includes the information as below picture.
Here is what you can do: you will have your contact
id
so base on that you can save details like phone numbers ,emails in your custom object and use that object to display details
Custom class like this:
class Contact
{
int contactId;
String Name;
ArrayList<Phone> phone;
ArrayList<Email> emails;
}
class Phone
{
String PhoneType;
String number;
}
class Email
{
String type;
String EmailId;
}
Retrieve detail and add it to your custom class:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
//
// Get all email addresses.
//
Cursor emails = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
String email = emails.getString(emails.getColumnIndex(Email.DATA));
int type = emails.getInt(emails.getColumnIndex(Phone.TYPE));
switch (type) {
case Email.TYPE_HOME:
// do something with the Home email here...
break;
case Email.TYPE_WORK:
// do something with the Work email here...
break;
}
}
emails.close();
}
cursor.close();
Hope this will help
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