Android get a cursor only with contacts that have an email listed android 2.0
I solved this one, here is how its done:
UPDATE
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
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 + " COLLATE NOCASE";
String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;
return mContent.query(Email.CONTENT_URI,
PROJECTION, filter, null, order);
Get only email address from contact list Android
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.
Get contact names with email address
Try following code
private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
...
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null) {
try {
final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
long contactId;
String displayName, address;
while (cursor.moveToNext()) {
contactId = cursor.getLong(contactIdIndex);
displayName = cursor.getString(displayNameIndex);
address = cursor.getString(emailIndex);
...
}
} finally {
cursor.close();
}
}
Checking if contact has an email address without a second query
After thinking for a while a realized I only need the email of the contact that you click on, so I just removed the email code from the asynctask
and made another asynctask
that loads an email of a specific contact. Then I get the email of the contact that was clicked and fill the email field. Code can be found in the repository I linked to in the question.
Related Topics
How to Create Multiple Pendingintents with the Same Requestcode and Different Extras
Android Notification with Buttons on It
Android Sax Parser Not Getting Full Text from Between Tags
Edittext Settext Not Working with Fragment
Listview with Choice_Mode_Multiple Using Checkedtext in a Custom View
How to Add Parameters to API (Http Post) Using Okhttp Library in Android
Folder Added on Android Is Not Visible via Usb
Adding Multiple Markers in Google Maps API V2 Android
Send Request Over Wifi (Without Connection) Even If Mobile Data Is on (With Connection) on Android M
No Peer Certificate' Error in Android 2.3 But Not in 4
Using Webview Sethttpauthusernamepassword
Android Repeat Action on Pressing and Holding a Button
How to Handle System_Alert_Window Permission Not Being Auto-Granted on Some Pre-Marshmallow Devices