排序的联系人列表有重复,为什么?

问题描述 投票:5回答:7

我已经将我的手机联系人排序并列入了一个arraylist但是,我在列表中找到了许多相同联系人姓名的副本。这种情况怎么样?怎么避免这个?

这是我试过的,

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

谁能帮忙?

android sorting android-contacts android-cursor
7个回答
3
投票

这里似乎没有人回答你的问题。

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

在Android中有3个主要表:

  1. Contacts表 - 每个联系人有一个项目
  2. RawContacts表 - 每个帐户有一个项目每个联系人(如谷歌,Outlook,Whatsapp等) - 多个RawContacts链接到单个Contact
  3. Data表 - 每个细节有一个项目(姓名,电子邮件,电话,地址等) - 每个数据项链接到一个RawContact,并且多个Data行链接到每个RawContact

您正在查询CommonDataKinds.Phone.CONTENT_URI这是Data表的一部分,所以如果联系人有多个电话,和/或它有来自多个来源的同一部电话(例如Google和Whatsapp),您将获得相同的电话相同的CONTACT_ID多一次。

解决方案是,使用HashMap(而不是HashSet),其中键是CONTACT_ID,因此您可以显示每个联系人的多个电话:

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

3
投票

你可以试试HashSet

公共类HashSet扩展AbstractSet实现Set,Cloneable,Serializable

  • 重复 不允许使用值。

代码结构

 HashSet<String> hashSET = new HashSet<String>();
        hashSET.add("AA");
        hashSET.add("BB");
        hashSET.add("CC");
        hashSET.add("AA"); // Adding duplicate elements

然后

Iterator<String> j = hashSET.iterator();
        while (j.hasNext())
            System.out.println(j.next()); // Will print "AA" once.
    }

现在SORT使用Hashset你的TreeSet值。

TreeSet实现SortedSet接口,因此不允许重复值。

 TreeSet<String> _treeSET= new TreeSet<String>(hashSET);

2
投票

可能在您的联系人中有多个群组,该群组将是WhatsApp,Google等。转到您的联系人并搜索具有whatsApp帐户的联系人。将显示与不同组的双重输入

你应该使用或更改你的ContactsBean,在你的Bean使用HashSet

注意:HashSet可以避免重复输入more

HashSet只包含独特的元素,它可以避免HashSet相同的关键元素

示例bean

public class ContactBean {
    private HashSet<String> number = new HashSet<String>(); 

    public void setNumber(String number) {
        if (number == null)
            return; 
        this.number.add(number.trim()); 
    }

    public HashSet<String> getNumber() {
        return this.number; 
    }
}

简单的例子

//Creating HashSet and adding elements  

  HashSet<String> hashSet=new HashSet<String>();  
  hashSet.add("Dhruv");  
  hashSet.add("Akash");  
  hashSet.add("Dhruv");   //Avoiding this entry   
  hashSet.add("Nirmal");  

 //Traversing elements  

  Iterator<String> itr = hashSet.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());
} 

1
投票

您可以使用HashSet来避免重复: -

HashSet<String> hset = 
               new HashSet<String>();

你可以在ArrayList中添加像HashSet: -

hset.add(your_string);

要么

转换你的ArrayListHashSet: -

Set<String> set = new HashSet<String>(your_arraylist_object);

HashSet避免重复输入:)


1
投票

我不知道你为什么从联系人那里得到重复的项目,也许手机联系人已经有重复的值。你可以在通讯录应用程序中查看。 您应始终使用设置数据结构,以避免重复项目。你可以找到更好的解释和示例here


0
投票

我认为你的重复是由于Whatsapp联系人干扰了联系。所以你可以使用这样的东西

                      String lastPhoneName = "";
                     String lastPhoneNumber = "";

                    //IN YOUR CONTACT FETCHING WHILE LOOP , INSIDE TRY 
                   String contactName = c.getString(c
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String phNumber = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    if (!contactName.equalsIgnoreCase(lastPhoneName) && !phNumber.equalsIgnoreCase(lastPhoneNumber)) {

                        lastPhoneName = contactName;
                        lastPhoneNumber = phNumber;

                        ContactModel model = new ContactModel();
                        model.setContact_id(contactid);
                        model.setContact_name(contactName.replaceAll("\\s+", ""));
                        model.setContact_number(phNumber.replaceAll("\\D+", ""));


                        list_contact_model.add(model);

                    } 

这将检查前一个号码是否与旧号码相同而不是跳过它。我希望你得到答案


0
投票

HashSet在键/值对中添加项目,并从项目集中删除重复的条目。

List<String> phone_num_list= new ArrayList<>();
// add elements to phone_num_list, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(phone_num_list);
phone_num_list.clear();
phone_num_list.addAll(hs);

快乐的编码!!

© www.soinside.com 2019 - 2024. All rights reserved.