12

请看一看 :-

 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 但我只想要一个记录 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;
    }
4

7 回答 7

5

你应该在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;
    }
}

并且无需关心重复。这会关心所有的事情..

于 2013-11-07T07:12:01.227 回答
5

这是我的工作

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);
    }
于 2016-01-06T05:42:44.147 回答
3

将您的联系人保存在 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);
于 2013-11-07T07:08:24.823 回答
2

通过像这样更改您的方法,您可以获得唯一一个不重复的联系人..

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;
}
于 2013-11-07T07:29:07.160 回答
0

使用 HashMap 因为 HashMap 不允许重复键

声明一个全局变量

// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();

在您的循环中将数据添加到 HashMap

// Enter Into Hash Map
nameEmailMap.put(email, name);

循环外获取日志

// Get The Contents of Hash Map in Log
        for (Map.Entry<String, String> entry : namePhoneMap.entrySet()) {
            String key = entry.getKey();
            Log.d(TAG, "Phone :" + key);
            String value = entry.getValue();
            Log.d(TAG, "Name :" + value);
        }

有关如何检索没有重复的联系人的完整答案,请参阅https://stackoverflow.com/a/54227282/3904109(对于电子邮件)和https://stackoverflow.com/a/54228199/3904109(对于电话)

于 2019-01-17T02:59:59.043 回答
0

该解决方案将:

  • 删除重复项

  • 删除所有不必要的开头,即目前它只允许以 03、92 和 +92 开头的数字

  • 显示所有同名的电话号码,例如 John 有 2 个号码。

     fun fetchContacts(): List<UserObject> {\
              val contacts: ArrayList<UserObject> = ArrayList()
              val mobileNoSet = HashSet<String>()
              val cursor= requiredContext()?.contentResolver?.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI),null,null,"DISPLAY_NAME ASC")
    
        while (cursor!!.moveToNext()) {
             val index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
             val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
             val photoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)
             val strName = cursor.getString(index)
             var number = cursor.getString(numberIndex)
             number = number.replace(" ", "")
             val photo = cursor.getString(photoIndex)
             if ((number.startsWith("03") || number.startsWith("92")||number.startsWith("+92")) && !mobileNoSet.contains(number)) {
                    contacts.add(UserObject(photo,strName,number))mobileNoSet.add(number)
                }
         }
        return contacts
      }
    
于 2020-08-13T09:38:56.650 回答
-1

一旦在您的 ArrayList 中添加了联系电子邮件continue,然后使用HashSet而不是 ArrayList

if (email != null) {
    contactList.add(contactsEntityBean);
    continue;
}
于 2013-11-07T07:12:55.223 回答