62

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?

Krishna Suthar
  • 3,071
  • 6
  • 31
  • 37

7 Answers7

123

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.

zundi
  • 2,361
  • 1
  • 28
  • 45
Marcin
  • 2,399
  • 3
  • 17
  • 15
78
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.

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
3
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();
              }

          }
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
2

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.

DariusL
  • 4,007
  • 5
  • 32
  • 44
0

If you can already get the contacts and their email address (if it exists), why don't you just remove the contacts with no email address from your list?

See here for more information on the Android Contacts API.

lrAndroid
  • 2,834
  • 18
  • 27
  • Hey thanks IrAndroid. Yes I am doing that only. But I am getting all contact names. Can you please show me some code for that? – Krishna Suthar Apr 12 '12 at 03:42
  • Here's a similar question to what you are asking: http://stackoverflow.com/questions/5205999/android-get-a-cursor-only-with-contacts-that-have-an-email-listed-android-2-0. – lrAndroid Apr 12 '12 at 03:43
  • Hey I tried that code given on this link. But I am getting email address. Not name. I want to get contact names if email address is exist. Can you give me some code? So that I can get some Idea. – Krishna Suthar Apr 12 '12 at 04:37
0

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

Aneh Thakur
  • 1,072
  • 12
  • 20
0

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 mahaXXXX@gmail.com->Mahatma Gandhi instead of Mahatma Gandhi->mahaXXXX@gmail.com

DragonFire
  • 3,722
  • 2
  • 38
  • 51