0

I have a custom list which has objects of class with name and phone number.. Now the problem is that there are too many duplicates . I need to remove all duplicates based on phone number.

When i tried this approach :

     list = getContacts();
    list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list));
    listView.setAdapter(new ContactsAdapter(this, list));  

ModelContact.java

        public class ModelContact {
            private String name;
            private String ContactNumber;
            private Bitmap imageUri;
            private String _id;
            public ModelContact() {
            }

            public Bitmap getImageUri() {
                return imageUri;
            }

            public void setImageUri(Bitmap imageUri) {
                this.imageUri = imageUri;
            }

            public String get_id() {
                return _id;
            }

            public void set_id(String _id) {
                this._id = _id;
            }



            public String getContactNumber() {
                return ContactNumber;
            }

            public void setContactNumber(String contactNumber) {
                ContactNumber = contactNumber;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
         @Override
     public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    if (obj instanceof ModelContact) {
        ModelContact temp = (ModelContact) obj;
        if (this.ContactNumber == temp.ContactNumber)
            return true;
    }
    return false;

}

 @Override
  public int hashCode() {
    // TODO Auto-generated method stub

    return (this.ContactNumber.hashCode());
}


        }

ContactsList:

  public class ContactsList extends ActionBarActivity {
        private ListView listView;
        ProgressBar progressBar;
        List<ModelContact> list;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_contacts);
            listView = (ListView) findViewById(R.id.contacts_list);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            list = new ArrayList<>();
            list = getContacts();
            list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list));
            listView.setAdapter(new ContactsAdapter(this, list));
        }

                private List<ModelContact> getContacts() {

                    ContentResolver cr = getContentResolver();
                    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                            null, null, null, null);
                    List<ModelContact> modelContacts = new ArrayList<>();
                    if (cur.getCount() > 0) {
                        ModelContact obj = new ModelContact();
                        while (cur.moveToNext()) {
                            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                            if (Integer.parseInt(cur.getString(
                                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                                Cursor pCur = cr.query(
                                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                        new String[]{id}, null);
                                while (pCur.moveToNext()) {
                                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    Bitmap bmp = retrieveContactPhoto(id);
                                    if (bmp == null) {
                                        System.out.println(name + "is \t null");
                                    } else {
                                        Toast.makeText(this, "not null \t" + phoneNo, Toast.LENGTH_SHORT).show();
                                    }
                                    String imagecon = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    obj.set_id(id);
                                    obj.setName(name);
                                    obj.setContactNumber(phoneNo);
                                    obj.setImageUri(bmp);
                                    modelContacts.add(obj);
                                    System.out.println("Name: " + name + ", Phone No: " + phoneNo);
                                    //Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                                }
                                pCur.close();
                            }
                        }
                    }
                    System.out.print("Size:::" + modelContacts.size());

                    return modelContacts;
                }

        private Bitmap retrieveContactPhoto(String contactID) {

            Bitmap photo = null;

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

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

                if (inputStream != null)
                    inputStream.close();

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


    }
Nikhil Verma
  • 1,777
  • 1
  • 20
  • 41

1 Answers1

0

First you need to override equals and hashcode methods inside your ModelContact class. You need to take care for phone number while overriding equals and hashcode methods.

Then you can use below code to remove duplicate elements

list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list))
ManojP
  • 6,113
  • 2
  • 37
  • 49