Android-删除重复联系人

Android-删除重复联系人,android,contacts,Android,Contacts,我试图显示在回收视图中的联系人一切正常,但联系人显示两次或三次 这是代码 RecyclerView recyclerView; List<Contacts> contactsList; ContactsAdapter adapter; public void getContactList() { Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONT

我试图显示在回收视图中的联系人一切正常,但联系人显示两次或三次

这是代码

 RecyclerView recyclerView;
 List<Contacts> contactsList;
 ContactsAdapter adapter;

 public void getContactList() {
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Contacts contacts = new Contacts(name, number);
        if (contactsList.contains(contacts)){
            cursor.moveToNext();
        }
        else {
            contactsList.add(contacts);
        }
        adapter = new ContactsAdapter(contactsList, getApplicationContext());
        recyclerView.setAdapter(adapter);

        adapter.notifyDataSetChanged();
    }
}
RecyclerView-RecyclerView;
列出联系人名单;
接触式适配器;
public void getContactList(){
Cursor Cursor=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
空,空,空,
“上部(“+Contacts.Contacts.DISPLAY_NAME+”)ASC”);
while(cursor.moveToNext()){
字符串名称=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.number));
联系人=新联系人(姓名、号码);
if(联系人列表包含(联系人)){
cursor.moveToNext();
}
否则{
联系人列表。添加(联系人);
}
适配器=新的ContactsAdapter(contactsList,getApplicationContext());
recyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}

那么,如何解决这个问题呢?

只传递phoneNumber作为键,传递phoneName作为值 您可以使用:

Map<String, String> map = new HashMap<>();
map.put("A", "1");
...
System.out.printf("Before: %s%n", map);

// Set in which we keep the existing values
Set<String> existing = new HashSet<>();
map = map.entrySet()
    .stream()
    .filter(entry -> existing.add(entry.getValue()))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.printf("After: %s%n", map);  

您可以按照本页上的这些步骤进行操作,在这里我可以提供完整的解决方案,我认为您应该考虑几个问题。是查询错误还是我保存联系人的方式错误

在您的应用程序中,是否应允许复制联系人?如果没有,您可以将
唯一约束添加到迁移中。看

如果您的数据库应该接受重复的值,那么您应该添加一列来表示该用户的唯一值?Ie userId列或联系人关联的位置,然后将其作为Selection和SelectionAgs添加到查询中

最后,如果你只是在寻找一个快速修复。。。你应该更深入地进行过滤

您的代码
contactsList.contacts(contacts)
永远不会为真,因为您总是在上面的行中创建联系人。然后检查列表是否包含新创建的列表。否则,您可以使用上述解决方案

您可以重写类联系人中的equals()方法。这样,如果覆盖方法中的两个联系人对象在逻辑上相等,则contactList.contains(Contact c)的计算结果将为true,因此它不会再次将其添加到列表中

     @Override
     public boolean equals(Object o){
          if(!o.getClass().equals(Contacts.class)) return false;
          Contact c2 = (Contact)o;
          // if phonenumber is unique to differentiate contact
          return c2.number.equals(number);
      }

您可以使用哈希集更改列表。HashSet防止有重复项。您的最终代码应该是这样的:

 RecyclerView recyclerView;
 HashSet<Contacts> contactsSet;
 ContactsAdapter adapter;

 public void getContactList() {
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Contacts contacts = new Contacts(name, number);
        contactsSet.add(contacts);
    }
    List<Contacts> contactsList = new ArrayList<>();
    adapter = new ContactsAdapter(contactsList, getApplicationContext());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
RecyclerView-RecyclerView;
HashSet-contactsSet;
接触式适配器;
public void getContactList(){
Cursor Cursor=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
空,空,空,
“上部(“+Contacts.Contacts.DISPLAY_NAME+”)ASC”);
while(cursor.moveToNext()){
字符串名称=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.number));
联系人=新联系人(姓名、号码);
联系人设置。添加(联系人);
}
列表联系人列表=新的ArrayList();
适配器=新的ContactsAdapter(contactsList,getApplicationContext());
recyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}

那么,我应该传递姓名和号码还是传递联系人对象?您没有在任何地方清除列表。。。可能函数不止一次发生,并且值被一次又一次地添加到列表中?
 RecyclerView recyclerView;
 HashSet<Contacts> contactsSet;
 ContactsAdapter adapter;

 public void getContactList() {
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Contacts contacts = new Contacts(name, number);
        contactsSet.add(contacts);
    }
    List<Contacts> contactsList = new ArrayList<>();
    adapter = new ContactsAdapter(contactsList, getApplicationContext());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}