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

前端之家收集整理的这篇文章主要介绍了如何从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);
        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,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\u0026amp; abc@gmail.com
但我只想要一个记录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);
            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,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;
    }

解决方法

你应该像下面这样修改你的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; 
    }
}

将关心重复的电子邮件…您可以使用相同的逻辑地址,手机等.

用以下代码替换你的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;
    }
}

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

原文链接:https://www.f2er.com/android/312956.html

猜你在找的Android相关文章