Himani
Himani

Reputation: 227

how to remove duplicate contacts from arraylist

I have created an app in which I am getting the contacts from a device. But I want to remove the duplicate contacts from the results.

How could I do it?

MainActivity

public class MainActivity extends Activity implements OnItemClickListener {

EditText searchText;

ArrayList<String> phno0 = new ArrayList<String>();
List<String> arrayListNames;
public List<ProfileBean> list;
public SearchableAdapter adapter;
//ProfileBean bean;
String[] cellArray = null;
String contacts;
ListView lv;
String phoneNumber, name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();


    lv = (ListView) findViewById(R.id.listview);
    list = new ArrayList<ProfileBean>();
    getAllCallLogs(this.getContentResolver());
    adapter = new SearchableAdapter(getApplication(), list);
    lv.setAdapter(adapter);
    lv.setItemsCanFocus(false);
    lv.setOnItemClickListener(this);
    lv.setTextFilterEnabled(true);


}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

}

public void getAllCallLogs(ContentResolver cr) {

    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                    + " ASC");
    while (phones.moveToNext()) {
        phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        list.add(new ProfileBean(name, phoneNumber));

    }
    phones.close();
}
}

Upvotes: 0

Views: 2576

Answers (5)

mehul chauhan
mehul chauhan

Reputation: 1802

Try to get ContactsContract.Contacts.NAME_RAW_CONTACT_ID) its unique id and its used for update contacts compare your contacts with raw id is same or not as below

    private void getAllContactsBackground() {

    ContentResolver contentResolver = getActivity().getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));

                Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));
                Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);


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


                while (cursorInfo.moveToNext()) {
                    ContactsModel info = new ContactsModel();
                    info.contacts_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    info.contacts_raw_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
                    info.contacts_name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    info.contacts_mobile = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceFirst("[^0-9]" + "91", "");
                    info.contacts_photo = photo;
                    info.contacts_photoURI = String.valueOf(pURI);


                    Cursor emailCur = contentResolver.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (emailCur.moveToNext()) {

                        info.contacts_email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        Log.e("email==>", "" + info.contacts_email);

                    }
                    emailCur.close();


                    int flag = 0;
                    if (arrayListAllContacts.size() == 0) {
                        arrayListAllContacts.add(info);
                    }
                    for (int i = 0; i < arrayListAllContacts.size(); i++) {

                        if (!arrayListAllContacts.get(i).getContacts_raw_id().trim().equals(info.contacts_raw_id)) {
                            flag = 1;

                        } else {
                            flag = 0;
                            break;
                        }

                    }
                    if (flag == 1) {
                        arrayListAllContacts.add(info);
                    }


                }
                cursorInfo.close();

            }
        }
        cursor.close();

    }
}

Upvotes: 0

Gujjula Ramesh Reddy
Gujjula Ramesh Reddy

Reputation: 87

use below code list=removeDuplicates(list);

public List<ProfileBean> removeDuplicates(List<ProfileBean> list) {
    // Set set1 = new LinkedHashSet(list);
    Set set = new TreeSet(new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            if (((ProfileBean) o1).getName().equalsIgnoreCase(((ProfileBean) o2).getName()) &&
                    ((ProfileBean)o1).getPhoneNumber().equalsIgnoreCase(((ProfileBean)o2).getPhoneNumber())) {
                return 0;
            }
            return 1;
        }
    });
    set.addAll(list);

    final List newList = new ArrayList(set);
    return newList;
}

Upvotes: 0

Sanawaj
Sanawaj

Reputation: 384

Add the following checking in the place of list.add(new ProfileBean(name, phoneNumber)); before adding into list:

int flag = 0
if(list.size() == 0){
list.add(new ProfileBean(name, phoneNumber));
}
    for(int i=0;i<list.size();i++){

    if(!list.get(i).getProfileName().trim().equals(name)){
    flag = 1;

    }else{
     flag =0;
     break;

}

    }
if(flag == 1){
list.add(new ProfileBean(name, phoneNumber));
}

Upvotes: 1

Sujith
Sujith

Reputation: 7763

The following function can be used for removing duplicates from String ArrayList Change it according to your requirement

public ArrayList<String> listWithoutDuplicates(ArrayList<String> duplicateList) {

    // Converting ArrayList to HashSet to remove duplicates
    LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);

    // Creating Arraylist without duplicate values
    ArrayList<String> listWithoutDuplicates = new ArrayList<String>(listToSet);

    return listWithoutDuplicates;
}

Upvotes: 0

nKn
nKn

Reputation: 13761

  • If you want to get rid of duplicates, consider using a HashSet instead.

  • If you can't/don't want to use it, simply check before adding whether the contact is already there.

    if (!myList.contains(newContact))
      myList.add(newContact);
    

Upvotes: 5

Related Questions