-2

I have been working on an android app to show contacts in the list. I am using recyclerview and i am able to show the contacts with name and image in the list.

But my list contains the duplicate of contact numbers. I used the below code to remove the duplicates but it shows

9566191161 +919566191161 as two entries. (ie.,) Along with country code, it is showed as a separate entry.

I am using a POJO class and adding it to Adapter as a List. In Pojo Class, i used the coding to remove duplicates like

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

I used as "+91"+this.getContactNumber().contains(temp.getContactNumber()) validation in the equals method but it doesn't remove that duplicate.

Can you guys please help me in this error.

My code snippet

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

Happy new year guys !

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40

1 Answers1

0

You can compare with id of contact and create unique list.

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

Edit You have to create object and set value after then find index from 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);

Change pojo class :

@Override
public boolean equals(Object obj) {

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

}
  • Hi reshma,Thanks for ur ans. I am a beginner and i want to know on how to resolve my issue to acquire some basic knowledge. I just want to know on how to remove numbers with +91 duplicates on the equals method in pojo class. Can you please help me.. – Kanagalingam Jan 01 '18 at 09:20
  • I edited answer, please, check again. – Reshma Dhaduk Jan 01 '18 at 10:42