Skip to content Skip to sidebar Skip to footer

Duplicate Contacts Not To Show In Listiview

i am making an app to show contacts number in a Listview but it is giving duplicate contacts entries i wanted to only show one contact singly private void getContactList() {

Solution 1:

booleancheckexsistance(ContactsPlacer obj)
{
boolean test= true;
for(inti=0 ; i<names.size();i++)
{
if(names.get(i).getContactname().equals(obj.getContactname()))
{
    test = false;
    break;

}

}


return test;
}



privatevoidgetContactList() {
    ContentResolvercr= getContentResolver();
    Cursorcur= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

    if ((cur != null ? cur.getCount() : 0) > 0) {
        while (cur != null && cur.moveToNext()) {
            Stringid= cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            Stringname= cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));

            if (cur.getInt(cur.getColumnIndex(
                    ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                CursorpCur= cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        newString[]{id}, null);
                while (pCur.moveToNext()) {
                    StringphoneNo= pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));


                    ContactsPlacerobj=newContactsPlacer();
                    obj.setContactname(name);
                    obj.setContactnumber(phoneNo);
                    if(checkexsistance(obj))
                       names.add(obj);

                }
                pCur.close();
            }
        }
    }
    if(cur!=null){
        cur.close();
    }




}

Solution 2:

here use the contain method of the arraylist like this

publicArrayList<String> getNameEmailDetails(){
    ArrayList<String> names = newArrayList<String>();
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                    newString[]{id}, null);
            while (cur1.moveToNext()) {
                //to get the contact namesString name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e(TAG+"Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e(TAG+"Email", email);
                if(email!=null){
                   if(!names.contains(name))
                    names.add(name);
                }
            }
            cur1.close();
        }
    }
    return names;
}

Solution 3:

I was having same problem few days ago, and resolved the same using this code, Please give it a try, Let me know if further help needed.

Don't forget to ask for permission and mention in manifest "android.permission.READ_CONTACTS"

ContentResolver cr = getActivity().getContentResolver();
        String[] PROJECTION = newString[]{ContactsContract.RawContacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.PHOTO_ID,
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.RawContacts.VERSION};
        String order = "CASE WHEN "
                + ContactsContract.Contacts.DISPLAY_NAME
                + " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
                + ContactsContract.Contacts.DISPLAY_NAME
                + ", "
                + ContactsContract.CommonDataKinds.Phone.DATA
                + " COLLATE NOCASE";
        String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + ContactsContract.CommonDataKinds.Phone.TYPE +"=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
        Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, filter, null, order);

        for (int i = 0; i < cur.getColumnCount(); i++) {
            Timber.e("column " + i + "=" + cur.getColumnName(i));
        }

        if (cur.moveToFirst()) {
            do {
                String name = cur.getString(1);
                Stringnumber = cur.getString(4);

                number = Utils.removeExtraCharFromString(number);

                if (number.length() > 8) {
                    mContactNumbers = mContactNumbers + number + ",";
                    PhoneContacts phoneContacts = newPhoneContacts();
                    try {
                        phoneContacts.setName(URLEncoder.encode(name, "UTF-8"));
                        phoneContacts.setNumber(number);
                        phoneContacts.setNameInitial(String.valueOf(phoneContacts.getName().charAt(0)));

                        listOfContactsRaw.add(phoneContacts);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            } while (cur.moveToNext());
        }
        cur.close();

        newHandler().postDelayed(newRunnable() {
            @Overridepublicvoidrun() {
                Set set = newTreeSet(newComparator() {
                    @Overridepublic int compare(Object o1, Object o2) {

                        PhoneContacts phoneContacts1 = (PhoneContacts) o1;
                        PhoneContacts phoneContacts2 = (PhoneContacts) o2;
                        if (phoneContacts1.getNumber().equalsIgnoreCase(phoneContacts2.getNumber())) {
                            return0;
                        }
                        return1;
                    }
                });
                set.addAll(listOfContactsRaw);

                System.out.println("\n***** After removing duplicates *******\n");

                listOfContacts = newArrayList(set);

                // Set listOfContacts with your recycler view
            }
        }, 200);

Solution 4:

try this

if(email!=null){
    if(!names.contains(name)){
        names.add(name);
    }
}

Post a Comment for "Duplicate Contacts Not To Show In Listiview"