通过电话簿在我的App中多次显示一些联系人 [英] Showing some contacts multiple times in my App from phone book

查看:100
本文介绍了通过电话簿在我的App中多次显示一些联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我得到了三两次相同的联系人,这种情况发生在某些联系人而不是每个联系人上.在我的应用程序中,一切都按预期工作,但是当单击我的显示联系人"时,它显示三个相同的联系人,但在手机联系人中仅存储一次.我从我这边尝试了一切,但是无法解决这个问题,请问有什么机构可以帮助我.还是有其他替代方法.

I'm getting same contact three or two times in my app this happening with some contacts not with every contacts. In my app everything is working as expected but when clicking on show contact from my it's shows three time same contact but in mobile phone contact stored only one time. I tried everything from my side but not able to solve this can any body please help me. Or is there any alternative way for same.

这是我的代码:-

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

推荐答案

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

You're printing those "FetchContacts" logs for per contact per phone, so if a contact has multiple phones stored for her you'll see it printed multiple times (even if it's the same phone number).

如果您安装了类似Whatsapp的应用,那么几乎总是会看到每个联系人的所有电话号码重复,从而导致这些日志的打印次数超过每个联系人一次.

If you have an app like Whatsapp installed, then almost always you'll see all phone number duplicated for each contact causing those logs to be printed more then once per contact.

此外,这是通过电话获得联系的一种缓慢而痛苦的方式. 取而代之的是,您可以直接通过Phones.CONTENT_URI直接进行查询,并在数据库中获取所有电话,然后通过Contact-ID将它们映射为联系人:

Also, that's a slow and painful way of getting contacts w/ phones. Instead you can simply query directly over Phones.CONTENT_URI and get all phones in the DB, and map them out into contacts by 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.

这篇关于通过电话簿在我的App中多次显示一些联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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