Reputation: 3075
I want to display only those contact names whose email address is present. Otherwise that contact name should not be displayed in List. How can I do this? Can anybody please help me?
Upvotes: 62
Views: 50700
Reputation: 4082
Declare A Global Variable
// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();
Then Use The Function Below
private void getEmailIDs() {
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
// Loop Through All The Emails
while (emails.moveToNext()) {
String name = emails.getString(emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
// Enter Into Hash Map
nameEmailMap.put(email, name);
}
// Get The Contents of Hash Map in Log
for (Map.Entry<String, String> entry : nameEmailMap.entrySet()) {
String key = entry.getKey();
Log.d(TAG, "Email :" + key);
String value = entry.getValue();
Log.d(TAG, "Name :" + value);
}
emails.close();
}
Remember in the above example the key is email and value is name so read your contents like [email protected]>Mahatma Gandhi instead of Mahatma Gandhi->[email protected]
Upvotes: 0
Reputation: 2409
Here is my super fast query to pull email addresses. It is much faster than pulling all contact columns as suggested by other answers...
public ArrayList<String> getNameEmailDetails() {
ArrayList<String> emlRecs = new ArrayList<String>();
HashSet<String> emlRecsHS = new HashSet<String>();
Context context = getActivity();
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME
+ ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
if (cur.moveToFirst()) {
do {
// names comes in hand sometimes
String name = cur.getString(1);
String emlAddr = cur.getString(3);
// keep unique only
if (emlRecsHS.add(emlAddr.toLowerCase())) {
emlRecs.add(emlAddr);
}
} while (cur.moveToNext());
}
cur.close();
return emlRecs;
}
I tried the code provided by 'Agarwal Shankar' but it took about 4 seconds to get contacts on my test device, and this code took about 0.04 sec.
Upvotes: 124
Reputation: 4077
Another solution.
private static final Uri URI_CONTACT_DATA = ContactsContract.Data.CONTENT_URI;
private static final String COLUMN_EMAIL = ContactsContract.CommonDataKinds.Email.ADDRESS;
private static final String COLUMN_DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
private static final String COLUMN_MIMETYPE = ContactsContract.Data.MIMETYPE;
private static final String[] PROJECTION = {
COLUMN_DISPLAY_NAME,
COLUMN_EMAIL,
COLUMN_MIMETYPE
};
private Cursor getCursor() {
ContentResolver resolver = context.getContentResolver();
String selection = COLUMN_MIMETYPE + "=?";
final String[] selectionArgs = {ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
return resolver.query(URI_CONTACT_DATA, PROJECTION, selection, selectionArgs, null);
}
The issue is that the table at ContactsContract.Contacts.CONTENT_URI
holds the entire contact database. This includes phone numbers, emails, organisations, and even completely custom data, so you cannot use it without filtering with ContactsContract.Data.MIMETYPE
. A row in this database holds a value (or values, it has 15 generic columns) related to a certain account, so you might need to group them yourself. I needed this to autocomplete emails, so the format (email per row) was perfect.
Upvotes: 2
Reputation: 1100
Here is a simple way to get email id of contact from contact list. You need to pass contact id of user in below method and it will return you email id if exists
public String getEmail(String contactId) {
String emailStr = "";
final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE};
Cursor emailq = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[]{contactId}, null);
if (emailq.moveToFirst()) {
final int contactEmailColumnIndex = emailq.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (!emailq.isAfterLast()) {
emailStr = emailStr + emailq.getString(contactEmailColumnIndex) + ";";
emailq.moveToNext();
}
}
return emailStr;
}
And also if you want to learn how to get contact list in your app follow this link: show contact list in app android - trinitytuts
Upvotes: 0
Reputation: 34765
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id},null);
while (emailCur.moveToNext()) {
String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email",name+" "+email);
}
emailCur.close();
}
}
Upvotes: 3
Reputation: 34765
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
the above method return an arraylist of names which has email id.
Upvotes: 78