【问题标题】:Removing Duplicates from Contacts list [duplicate]从联系人列表中删除重复项 [重复]
【发布时间】:2018-01-01 08:41:49
【问题描述】:

我一直在开发一个在列表中显示 contacts 的 Android 应用程序。我正在使用recyclerview,我可以在列表中显示联系人的姓名和图片。

但我的列表包含重复的联系电话。我使用下面的代码删除了重复项,但它显示了

9566191161 +919566191161 作为两个条目。 (即,)与国家代码一起显示为单独的条目。

我正在使用 POJO 类并将其作为列表添加到适配器。在 Pojo Class 中,我使用编码来删除重复项,例如

  @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stubs
        if (obj instanceof ContactVO) {
            ContactVO temp = (ContactVO) obj;
            if (this.getContactNumber() == temp.getContactNumber() && this.getContactName() == temp.getContactName() && (this.getContactNumber()).contains(temp.getContactNumber()))
                return false;
        }
        return true;
    }

    @Override

    public int hashCode() {
        // TODO Auto-generated method stub
        return (this.getContactNumber().hashCode() + this.getContactName().hashCode());
    }
}

我在 equals 方法中用作 "+91"+this.getContactNumber().contains(temp.getContactNumber()) 验证,但它不会删除该重复项。

你们能帮我解决这个错误吗?

我的代码sn-p

 Cursor phones = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (phones.moveToNext()) {
            name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(" ", "");
            imageUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            System.out.println("Name and Phone number = " + name + phoneNumber + imageUri);


            contactVOList.add(new ContactVO(imageUri, name, phoneNumber));
            System.out.println("List size before removing duplicates =" + contactVOList.size());

        }

        Set<ContactVO> s = new HashSet<ContactVO>();
        s.addAll(contactVOList);
        contactVOList = new ArrayList<ContactVO>();
        contactVOList.addAll(s);


        // contactVOList = removeDuplicates(contactVOList);
        System.out.println("List size after removing duplicates =" + contactVOList.size());
        System.out.println("ListSize before sendinf" + contactVOList.size());
        mAdapter = new AllContactsAdapter(getContext(), contactVOList, userId, mobilenumber);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
        rvContacts.setLayoutManager(mLayoutManager);
        rvContacts.setItemAnimator(new DefaultItemAnimator());
        rvContacts.setAdapter(mAdapter);

大家新年快乐!

【问题讨论】:

  • 您可以将添加项目的位置的 sn-p 粘贴到列表中吗?
  • @SabidBinHabib 我已经添加了代码伙伴
  • 您的问题与stackoverflow.com/q/47786280/819355 相同,在接受的答案中,您可以找到使用Phone.CONTACT_ID 以避免重复的代码

标签: java android android-contacts


【解决方案1】:

您可以与联系人ID进行比较并创建唯一列表。

       ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);
        if (cursor != null) {
            try {
                final int idIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
                final int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                final int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                String name, phoneNo, id;
                while (cursor.moveToNext()) {
                    name = cursor.getString(nameIndex);
                    phoneNo = cursor.getString(numberIndex);
                    id = cursor.getString(idIndex);


                    Bitmap photo = null;

                    try {
                        InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                                ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));

                        if (inputStream != null) {
                            photo = BitmapFactory.decodeStream(inputStream);
                            inputStream.close();
                        }




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

                    //check for is same contact already added? - remove duplication
                    Contacts contacts = new Contacts();
                    contacts.setContactId(id);

                    int index = contactsArrayList.indexOf(contacts);
                    if(index == -1) { // not duplicate
                       // You can add contact here in list

                    }

                }
            } finally {
                cursor.close();
            }
        }

编辑 您必须创建对象并设置值,然后从 arraylist 中查找索引。

ContactVO contact = new ContactVO();
contact.setContactNumber(+91000000);
int index = contactVOList.indexOf(contact); // if record not contain in list then return -1 otherwise return index of list.

if(index != -1)
contactVOList.remove(index);

更改 pojo 类:

@Override
public boolean equals(Object obj) {

     return getContactNumber().equals(((ContactVO)obj).getContactNumber());

}

【讨论】:

  • 嗨 reshma,谢谢你的回答。我是初学者,我想知道如何解决我的问题以获取一些基本知识。我只想知道如何在 pojo 类的 equals 方法上删除 +91 重复的数字。你能帮帮我吗..
  • 我编辑了答案,请重新检查。
猜你喜欢
  • 2017-07-18
  • 2013-11-18
  • 2021-02-08
  • 2014-09-30
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多