Search code examples
androidlistviewcontacts

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() {
    ContentResolver cr = getContentResolver();
    Cursor cur = 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()) {
            String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));

            if (cur.getInt(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));
                    ContactsPlacer obj = new ContactsPlacer();
                    obj.setContactname(name);
                    obj.setContactnumber(phoneNo);
                    names.add(obj);

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




}

help me about it that the duplicate contacts not show in listview


Solution

  • boolean checkexsistance(ContactsPlacer obj)
    {
    boolean test= true;
    for(int i = 0 ; i<names.size();i++)
    {
    if(names.get(i).getContactname().equals(obj.getContactname()))
    {
        test = false;
        break;
    
    }
    
    }
    
    
    return test;
    }
    
    
    
    private void getContactList() {
        ContentResolver cr = getContentResolver();
        Cursor cur = 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()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));
    
                if (cur.getInt(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));
    
    
                        ContactsPlacer obj = new ContactsPlacer();
                        obj.setContactname(name);
                        obj.setContactnumber(phoneNo);
                        if(checkexsistance(obj))
                           names.add(obj);
    
                    }
                    pCur.close();
                }
            }
        }
        if(cur!=null){
            cur.close();
        }
    
    
    
    
    }