Sorted list of contacts having duplicates ,why?

前端 未结 7 1153
青春惊慌失措
青春惊慌失措 2020-12-07 02:26

I have sorted and listed my phone contacts in to an arraylist but ,i got many duplicates of same contact names in the list .How this happens? how to avoid this?

This

相关标签:
7条回答
  • 2020-12-07 03:03

    I think your duplication is because of Whatsapp contact interfering with contact . so you can use something like this

                          String lastPhoneName = "";
                         String lastPhoneNumber = "";
    
                        //IN YOUR CONTACT FETCHING WHILE LOOP , INSIDE TRY 
                       String contactName = c.getString(c
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phNumber = c
                                .getString(c
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
                        if (!contactName.equalsIgnoreCase(lastPhoneName) && !phNumber.equalsIgnoreCase(lastPhoneNumber)) {
    
                            lastPhoneName = contactName;
                            lastPhoneNumber = phNumber;
    
                            ContactModel model = new ContactModel();
                            model.setContact_id(contactid);
                            model.setContact_name(contactName.replaceAll("\\s+", ""));
                            model.setContact_number(phNumber.replaceAll("\\D+", ""));
    
    
                            list_contact_model.add(model);
    
                        } 
    

    this will check that previous number is same as old one than skip it . I hope You get your answer

    0 讨论(0)
提交回复
热议问题