在我的应用程序中多次显示来自 phone 本书的某些联系人

Showing some contacts multiple times in my App from phone book

我在我的应用程序中收到同一个联系人三两次,这种情况发生在某些联系人身上,而不是所有联系人身上。在我的应用程序中,一切都按预期工作,但是当点击我的显示联系人时,它显示了三次相同的联系人,但在移动 phone 中联系人只存储了一次。我从我这边尝试了一切,但无法解决这个问题,请任何人帮助我。或者是否有其他替代方法。

这是我的代码:-

    @Override
protected Integer doInBackground(Void... params) {


    try {


        db = new WhooshhDB(myContext);
        this.list = new ArrayList<>();

        ContentResolver cr = myContext.getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
        if ((cur != null ? cur.getCount() : 0) > 0) {

            while (cur != null && cur.moveToNext()) {

                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));

                if (cur.getInt(cur.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER));

                        ContactModel model = new ContactModel();

                        if (phoneNo.replaceAll("\s", "").trim().length() > 7) {
                            model.name = name;
                            model.mobileNumber = phoneNo.replaceAll("\s", "");
                            if (model.mobileNumber.contains("-")) {
                                model.mobileNumber = model.mobileNumber.replaceAll("-", "");
                            }
                            model.iconHexColor = AppConstant.getRandomSubscriptionHexColor(myContext);
                            if (!phoneNumber.equals(model.mobileNumber)) {
                                list.add(model);
                            }

                        }

                        Log.i("FetchContacts", "Name: " + name);
                        Log.i("FetchContacts", "Phone Number: " + phoneNo);
                    }
                    pCur.close();
                }
            }
        }
        if (cur != null) {
            cur.close();
        }

        return AppConstant.SUCCESS;
    } catch (Exception ex) {
        return null;
    }
}

去掉

while (cur != null && cur.moveToNext()) { 

改为

if(cur.moveToFirst()){
list.clear();

您正在为每个 phone 的每个联系人打印那些 "FetchContacts" 日志,因此如果一个联系人为她存储了多个 phone,您会看到它被打印多次(即使是相同的 phone 号码)。

如果您安装了 Whatsapp 之类的应用程序,那么您几乎总是会看到每个联系人的所有 phone 号码都是重复的,导致每个联系人打印这些日志不止一次。

此外,这是一种与 phone 取得联系的缓慢而痛苦的方式。 相反,您可以直接通过 Phones.CONTENT_URI 查询并获取数据库中的所有 phone,然后通过 Contact-ID:

将它们映射到联系人中
Map<String, List<String>> contacts = new HashMap<String, List<String>>();

String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = cr.query(Phone.CONTENT_URI, projection, null, null, null);

while (cur != null && cur.moveToNext()) {
    long id = cur.getLong(0); // contact ID
    String name = cur.getString(1); // contact name
    String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234

    Log.d(TAG, "got " + id + ", " + name + ", " + data);

    // add info to existing list if this contact-id was already found, or create a new list in case it's new
    String key = id + " - " + name;
    List<String> infos;
    if (contacts.containsKey(key)) {
        infos = contacts.get(key);
    } else {
        infos = new ArrayList<String>();
        contacts.put(key, infos);
    }
    infos.add(data);
}

// contacts will now contain a mapping from id+name to a list of phones.
// you can enforce uniqueness of phones while adding them to the list as well.