如何从android中的联系人列表中删除重复的联系人

请看一看 :-

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; } 

这个方法是从同一个用户返回多个联系人假设如果我已经存储了abc @ gmail.com,abc @ gmail.com为同一个用户,所以它是返回abc@gmail.com&abc@gmail.com,但我只想要一个loggingabc @ 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; } 

Solutions Collecting From Web of "如何从android中的联系人列表中删除重复的联系人"

你应该像下面那样修改你的ContactsEntityBean

 public class ContactsEntityBean { private HashSet<String> emails = new HashSet<String>(); public void setEmail(String email) { if (email == null) return; this.emails.add(email.trim()); } public HashSet<String> getEmails() { return this.emails; } } 

将关心重复的电子邮件…你可以使用相同的逻辑地址,电话等


用下面的代码replace您的ContactsEntityBean

 public class ContactsEntityBean { private HashSet<String> emails; private HashSet<String> phones; private HashSet<String> addresses; private String contactId; private boolean checked = false; public ContactsEntityBean() { this.emails = new HashSet<String>(); this.phones = new HashSet<String>(); this.addresses = new HashSet<String>(); } public HashSet<String> getPhones() { return phones; } public void setPhones(String phone) { if (phone == null) return; this.phones.add(phone.trim()); } public HashSet<String> getAddresses() { return addresses; } public void setAddresses(String address) { if (address == null) return; this.addresses.add(address.trim()); } public void setEmails(String email) { if (email == null) return; this.emails.add(email.trim()); } public HashSet<String> getEmails() { return emails; } public String getContactId() { return contactId; } public void setContactId(String contactId) { this.contactId = contactId; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } } 

而且不需要关心重复。 这将关心所有的事情..

通过改变你这样的方法,你可以得到唯一一个不重复的联系人..

 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); HashMap<String, String> data = new HashMap<String, String>(); 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)); if (!data.containsValue(email)) { // Log.e("Email", email); contactsEntityBean.setName(name); contactsEntityBean.setEmail(email); data.put(name, email); if (email != null) { contactList.add(contactsEntityBean); } } } cur1.close(); } } return contactList; } 

这是我的工作

 private void getContactDetails(ContentResolver contentResolver) { Cursor phones = contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); HashSet<String> mobileNoSet = new HashSet<String>(); while (phones.moveToNext()) { String name = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String email = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)); String imagUri = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_URI)); long id = phones.getColumnIndex(ContactsContract.Contacts._ID); if (!mobileNoSet.contains(phoneNumber)) { arrayContacts.add(new Contact(name, phoneNumber, email, imagUri, id)); mobileNoSet.add(phoneNumber); } } adapterContact = new AdapterContact(getActivity(), arrayContacts); listContact.setAdapter(adapterContact); } 

将联系人保存在ArrayList中 ,然后从列表中删除重复项。 删除重复元素的最简单方法是将内容添加到Set(不允许重复),然后将Set添加回ArrayList。

像这样使用HashSet

 ArrayList al = new ArrayList(); // add elements to al, including duplicates HashSet hs = new HashSet(); hs.addAll(al); al.clear(); al.addAll(hs); 

一旦在你的ArrayList中添加的联系电子邮件continue ,并使用HashSet而不是ArrayList

 if (email != null) { contactList.add(contactsEntityBean); continue; }