如何删除从Android的联系人列表中的重复接触 [英] how to remove duplicate contact from contact list in android

查看:137
本文介绍了如何删除从Android的联系人列表中的重复接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看: -

 公共静态的ArrayList< ContactsEntityBean> getContactDetails(
            上下文mContext){
        ArrayList的< ContactsEntityBean> contactList =新的ArrayList< ContactsEntityBean>();
        ContentResolver的CR = mContext.getContentResolver();
        光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,空,
                NULL,NULL,NULL);
        如果(cur.getCount()大于0){
            而(cur.moveToNext()){
                字符串ID = cur.getString(CUR
                        .getColumnIndex(ContactsContract.Contacts._ID));
                光标CUR1 = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        空,ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                +=?,新的String [] {
                            ID
                        }, 空值);
                而(cur1.moveToNext()){
                    ContactsEntityBean contactsEntityBean =新ContactsEntityBean();
                    //获取联系人姓名
                    字符串名称= CUR1
                            .getString(CUR1
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                    // Log.e(姓名:,姓名);
                    字符串email = CUR1
                            .getString(CUR1
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    // Log.e(电子邮件,电子邮件);
                    contactsEntityBean.setName(名称);
                    contactsEntityBean.setEmail(电子邮件);
                    如果(电子邮件!= NULL){
                        contactList.add(contactsEntityBean);
                    }
                }
                cur1.close();
            }
        }
        返回contactList;
    }
 

此方法是,如果我有存储ABC @ gmail.com,ABC @ gmail.com为相同的用户,以便它返回abc@gmail.com&来自同一用户的回报倍数接触假设; abc@gmail.com 但我想只有一个记录abc@gmail.com

 公共静态的ArrayList< SearchEntityBean> getContactEmailDetails(
            上下文mContext){
        ArrayList的< SearchEntityBean> contactList =新的ArrayList< SearchEntityBean>();


        尝试 {
            ContentResolver的CR = mContext.getContentResolver();
            光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,空,
                    NULL,NULL,NULL);
            如果(cur.getCount()大于0){
                而(cur.moveToNext()){
                    字符串email =;
                    字符串ID = cur.getString(CUR
                            .getColumnIndex(ContactsContract.Contacts._ID));

                    光标CUR1 = cr.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            空值,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                    +=?,新的String [] {
                                ID
                            }, 空值);
                    SearchEntityBean contactsEntityBean =新SearchEntityBean();
                    而(cur1.moveToNext()){

                        //获取联系人姓名

                        字符串名称= CUR1
                                .getString(CUR1
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        串图象= CUR1
                                .getString(CUR1
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
                        字符串的邮件= CUR1
                                .getString(CUR1
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                        如果(邮件!= NULL){
                            如果(!mail.equalsIgnoreCase(登录preferenceClass
                                    .getEmailID(mContext)))
                                电子邮件=邮件+邮件+,;
                        }
                        // Log.e(罗希特,电子邮件:+电子邮件);
                        contactsEntityBean.setName(名称);
                        contactsEntityBean.setImage(图像);

                    }

                    如果(电子邮件!= NULL){

                        如果(email.length()大于0){

                            如果(email.split(,)长度GT; 1){

                                contactsEntityBean.setMutipleEmail(真正的);

                            }

                            contactsEntityBean.setUserType(2);
                            contactsEntityBean.setContactId(ID);
                            contactsEntityBean.setEmail(电子邮件);
                            contactList.add(contactsEntityBean);
                        }
                    }
                    cur1.close();
                }
            }
        }赶上(例外五){
            e.printStackTrace();
        }
        HashSet的< SearchEntityBean> HS =新的HashSet< SearchEntityBean>();
        hs.addAll(contactList);
        contactList.clear();
        contactList.addAll(HS);
        返回contactList;
    }
 

解决方案

您应该修改 ContactsEntityBean 象下面这样

 公共类ContactsEntityBean {
    私人的HashSet<字符串>电子邮件=新的HashSet<字符串>();

    公共无效setEmail(字符串电子邮件){
        如果(电子邮件== NULL)
            返回;
        this.emails.add(email.trim());
    }

    公众的HashSet<字符串> getEmails(){
        返回this.emails;
    }
}
 

会在乎重复的电子邮件...您可以使用相同的逻辑地址,电话等。


更换你的 ContactsEntityBean 低于code

 公共类ContactsEntityBean {
    私人的HashSet<字符串>电子邮件;
    私人的HashSet<字符串>手机;
    私人的HashSet<字符串>地址;
    私人字符串的ContactID;
    私人布尔查= FALSE;

    公共ContactsEntityBean(){
        this.emails =新的HashSet<字符串>();
        this.phones =新的HashSet<字符串>();
        this.addresses =新的HashSet<字符串>();
    }

    公众的HashSet<字符串> getPhones(){
        回电话;
    }

    公共无效setPhones(字符串手机){
        如果(电话== NULL)
            返回;
        this.phones.add(phone.trim());
    }

    公众的HashSet<字符串> getAddresses(){
        返回地址;
    }

    公共无效setAddresses(字符串地址){
        如果(地址== NULL)
            返回;
        this.addresses.add(address.trim());
    }

    公共无效setEmails(字符串电子邮件){
        如果(电子邮件== NULL)
            返回;
        this.emails.add(email.trim());
    }

    公众的HashSet<字符串> getEmails(){
        返回的电子邮件;
    }

    公共字符串getContactId(){
        返回的ContactID;
    }

    公共无效setContactId(字符串的ContactID){
        this.contactId =的ContactID;
    }

    公共布尔器isChecked(){
        返回检查;
    }

    公共无效setChecked(布尔检查){
        this.checked =检查;
    }
}
 

和没有必要去关心重复。这会关心所有的东西..

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

解决方案

You should modify your ContactsEntityBean like below

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

Will care about duplicate emails... you can use same logic for addresses, phones etc.


Replace your ContactsEntityBean with below code

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

And no need to care about duplicates. this will care about all the things..

这篇关于如何删除从Android的联系人列表中的重复接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆