有重复的联系人排序列表,为什么? [英] Sorted list of contacts having duplicates ,why?

查看:116
本文介绍了有重复的联系人排序列表,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将电话联系人排序并列出到一个数组列表中,但是,列表中有很多相同联系人姓名的重复项.这是怎么发生的?如何避免这种情况?

I have sorted and listed my phone contacts in to an arraylist but ,i got many duplicates of same contact names in the list .How this happens? how to avoid this?

这是我尝试过的,

  cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
                "(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

  while (cursor.moveToNext()) {

        try {

            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contact_names_list.add(name);
            phone_num_list.add(phonenumber);


        } catch (Exception e) {
            e.printStackTrace();
        }

任何人都可以帮忙吗?

推荐答案

这里似乎没人回答您的问题.

No one here seems to answer your question.

看到重复联系人的原因是,您查询的是电话而不是联系人.

The reason you're seeing duplicate contacts is that you're querying for phones not contacts.

在Android中,有3个主表:

In Android there are 3 main tables:

  1. Contacts表-每个联系人有一个项目
  2. RawContacts表-每个联系人每个帐户只有一个项目(例如Google,Outlook,Whatsapp等)-多个RawContacts链接到一个Contact
  3. Data表-每个详细信息都有一个项目(名称,电子邮件,电话,地址等)-每个数据项都链接到单个RawContact,并且多个Data行链接到每个.
  1. Contacts table - has one item per contact
  2. RawContacts table - has one item per-contact per-account (such as Google, Outlook, Whatsapp, etc.) - multiple RawContacts are linked to a single Contact
  3. Data table - has one item per detail (name, email, phone, address, etc.) - each data item is linked to a single RawContact, and multiple Data rows are linked to each RawContact.

您正在查询作为Data表一部分的CommonDataKinds.Phone.CONTENT_URI,因此,如果某位联系人拥有一部以上的电话,并且/或者它具有来自多个来源(例如Google和Whatsapp)的同一部电话,会再次使用相同的CONTACT_ID来获得同一部手机.

You're querying on CommonDataKinds.Phone.CONTENT_URI which is a part of the Data table, so if a contact has more then one phone, and/or it has the same phone from multiple sources (e.g. Google and Whatsapp) you'll get the same phone with the same CONTACT_ID more then once.

解决方案是使用HashMap(而不是HashSet),其键为CONTACT_ID,这样您可以为每个联系人显示多部电​​话:

The solution would be, to use a HashMap (rather then a HashSet), where the key is CONTACT_ID, so you can display multiple phones per contact:

String[] projection = new String[] { CommonDataKinds.Phone.CONTACT_ID, CommonDataKinds.Phone.DISPLAY_NAME, CommonDataKinds.Phone.NUMBER };
Cursor cursor = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);

HashMap<Long, Contact> contacts = new HashMap<>();

while (cursor.moveToNext()) {
    long id = cursor.getLong(0);
    String name = cursor.getString(1);
    String phone = cursor.getString(2);

    Contact c = contacts.get(id);
    if (c == null) {
        // newly found contact, add to Map
        c = new Contact();
        c.name = name;
        contacts.put(id, c);
    }

    // add phone to contact class
    c.phones.add(phone);
}
cursor.close();


// simple class to store multiple phones per contact
private class Contact {
    public String name;
    // use can use a HashSet here to avoid duplicate phones per contact
    public List<String> phones = new ArrayList<>(); 
}

如果要按名称对HashMap进行排序:

List<Contact> values = new ArrayList<>(contacts.values());
Collections.sort(values, new Comparator<Contact> {
    public int compare(Contact a, Contact b) {
        return a.name.compareTo(b.name);
    }
});

// iterate the sorted list, per contact:
for (Contact contact : values) {
    Log.i(TAG, "contact " + contact.name + ": ");
    // iterate the list of phones within each contact:
    for (String phone : contact.phones) {
        Log.i(TAG, "\t phone: " + phone);
    }
}

这篇关于有重复的联系人排序列表,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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