please have a look :-
public static ArrayList<ContactsEntityBean> getContactDetails(
Context mContext) {
ArrayList<ContactsEntityBean> contactList = new ArrayList<ContactsEntityBean>();
ContentResolver cr = mContext.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()) {
ContactsEntityBean contactsEntityBean = new ContactsEntityBean();
// 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);
contactsEntityBean.setName(name);
contactsEntityBean.setEmail(email);
if (email != null) {
contactList.add(contactsEntityBean);
}
}
cur1.close();
}
}
return contactList;
}
this method is return multiple contact from same user suppose if i have stored abc@gmail.com,abc@gmail.com for same user so it is returning abc@gmail.com& abc@gmail.com
but i want only one record abc@gmail.com
public static ArrayList<SearchEntityBean> getContactEmailDetails(
Context mContext) {
ArrayList<SearchEntityBean> contactList = new ArrayList<SearchEntityBean>();
try {
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String email = "";
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);
SearchEntityBean contactsEntityBean = new SearchEntityBean();
while (cur1.moveToNext()) {
// to get the contact names
String name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String image = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
String mail = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (mail != null) {
if (!mail.equalsIgnoreCase(LoginPreferenceClass
.getEmailID(mContext)))
email = email + mail + ",";
}
// Log.e("rohit", "Contact Email :" + email);
contactsEntityBean.setName(name);
contactsEntityBean.setImage(image);
}
if (email != null) {
if (email.length() > 0) {
if (email.split(",").length > 1) {
contactsEntityBean.setMutipleEmail(true);
}
contactsEntityBean.setUserType("2");
contactsEntityBean.setContactId(id);
contactsEntityBean.setEmail(email);
contactList.add(contactsEntityBean);
}
}
cur1.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
HashSet<SearchEntityBean> hs = new HashSet<SearchEntityBean>();
hs.addAll(contactList);
contactList.clear();
contactList.addAll(hs);
return contactList;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…