Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only email address from contact list Android

Tags:

android

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?

like image 689
Krishna Suthar Avatar asked Apr 12 '12 03:04

Krishna Suthar


People also ask

How to show only Google Contacts in Android?

Open your Contacts app and tap the Options button (three dots), and select Contacts Manager. On the next screen, tap on Contacts to display from the menu. Next, if you only want contacts with a phone number, tap on Phone. You can also customize it by selecting just the Google account you want to see contacts from.

How do I select a contact list on Android Contacts?

Contact someone On your Android phone or tablet, open the Contacts app . Tap a Contact in the list. Select an Option.


2 Answers

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.

like image 83
Marcin Avatar answered Sep 30 '22 14:09

Marcin


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.

like image 27
Shankar Agarwal Avatar answered Sep 30 '22 13:09

Shankar Agarwal