0

help me to fix this problem to remove duplicate contact that i get from Database

 cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                 ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''",null,"upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
marmor
  • 27,641
  • 11
  • 107
  • 150
  • Possible duplicate of [Sorted list of contacts having duplicates ,why?](https://stackoverflow.com/questions/47786280/sorted-list-of-contacts-having-duplicates-why) – marmor Feb 01 '18 at 09:14
  • you're querying over the phones table, so you're getting a row per phone even if they're the same phone and belong to the same contact, see my answer in the link above for a solution – marmor Feb 01 '18 at 09:16

1 Answers1

0

use this code to contact list from mobile database and set on list view

No duplicate data retreive.

public void customgetContactList()
{
    String SELECTION = "((" +
            ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
            ContactsContract.Data.DISPLAY_NAME + " != '"+checkDuplicacy+"' ))";


    Cursor cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
            SELECTION,null,"upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

    int ColumeIndex_ID = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
        int ColumeIndex_DISPLAY_NAME = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int ColumeIndex_HAS_PHONE_NUMBER = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    ArrayList<HashMap<String,String>> arrayList=new ArrayList<>();
    while (cursor.moveToNext()){
        String id = cursor.getString(ColumeIndex_ID);
        String name = cursor.getString(ColumeIndex_DISPLAY_NAME);
        String has_phone = cursor.getString(ColumeIndex_HAS_PHONE_NUMBER);

        //phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        if (!name.equals(checkDuplicacy) && !has_phone.equals(checkDuplicacynumber))
        {

            HashMap<String,String> hashMap=new HashMap<>();//create a hashmap to store the data in key value pair
            hashMap.put("name",name);
            hashMap.put("phone",has_phone);
            arrayList.add(hashMap);
            String[] from = {"name","phone"};
            int[] to = {R.id.textViewName,R.id.textviewnumber};
            SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.customtextviewforlist,from,to);
          //  SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.customtextviewforlist,cursor,from,to);

            //startManagingCursor(cursor);
            contactListView.setAdapter(simpleAdapter);
            contactListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


            checkDuplicacy=name;
            checkDuplicacynumber=has_phone;
        }
    }
}
  • although this might return proper results for most setups, if you have 2 contacts with the same name on your phone, it'll skip one of them. also, if you have a contact with more then one phone number, it'll only display the first phone for that contact. in general I would refrain from using `DISPLAY_NAME` as a unique key in Android Contacts. – marmor Feb 04 '18 at 07:00
  • the answer here: https://stackoverflow.com/a/47788324/819355 uses CONTACT_ID field to de-dup results, and also supports multiple phones per contact. This question is an exact duplicate of the one on the link – marmor Feb 04 '18 at 07:02
  • Thanks buddy. With your help i able to fix it . – Harwinder Singh Feb 05 '18 at 09:28