Android get phone contacts and remove duplicates

后端 未结 5 2034
天命终不由人
天命终不由人 2021-01-14 11:53

I am having an issue related to contacts. I got the phone contacts and stored them in my list object. Here\'s the code for it

  Uri uri = ContactsContract.Da         


        
5条回答
  •  日久生厌
    2021-01-14 12:35

    Here the code below is to find the duplicate number in your contact list and also the frequency that how many the number occur in your contact list

    public ArrayList findDuplicates(ArrayList listContainingDuplicates) {
        ArrayList duplicatesOrganised = new ArrayList();
        ArrayList setToReturn = new ArrayList();
    
        // Collections.sort(listContainingDuplicates);
    
        Collections.sort(listContainingDuplicates, new Comparator() {
            public int compare(Contact obj1, Contact obj2) {
                // ## Ascending order
                return obj1.getPhoneNumber().compareToIgnoreCase(obj2.getPhoneNumber()); // To compare string values
                // return Integer.valueOf(obj1.empId).compareTo(Integer.valueOf(obj2.empId)); // To compare integer values
    
                // ## Descending order
                // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values
                // return Integer.valueOf(obj2.empId).compareTo(Integer.valueOf(obj1.empId)); // To compare integer values
            }
        });
    
        int ii, size = listContainingDuplicates.size();
    
        //Orders all the duplicates together along with the unique(non-duplicate)
        for (ii = 0; ii < size; ii++) {
            if (ii + 1 == size) {
                duplicatesOrganised.add(listContainingDuplicates.get(ii));
                Log.i("DuplicateOrdered: ", listContainingDuplicates.get(ii).getPhoneNumber() + " " + listContainingDuplicates.get(ii).getName());
            } else if (listContainingDuplicates.get(ii).getPhoneNumber().equals(listContainingDuplicates.get(ii + 1).getPhoneNumber())) {
                duplicatesOrganised.add(listContainingDuplicates.get(ii));
                Log.i("DuplicateOrdered: ", listContainingDuplicates.get(ii).getPhoneNumber() + " " + listContainingDuplicates.get(ii).getName());
            } else {
                duplicatesOrganised.add(listContainingDuplicates.get(ii));
                Log.i("DuplicateOrdered: ", listContainingDuplicates.get(ii).getPhoneNumber() + " " + listContainingDuplicates.get(ii).getName());
            }
        }
        int firstcome = 0;
        int start = 0;
        boolean present = false;
        boolean duplicatefond = false;
        int startsetToReturn = 0;
        if (!duplicatesOrganised.isEmpty() &&
                duplicatesOrganised.size() > 1 &&
                !duplicatesOrganised.get(0).getPhoneNumber().equals(duplicatesOrganised.get(1).getPhoneNumber())) {
            start = 1;
        }
    
        for (int i = 0; i < duplicatesOrganised.size(); i++) {
            String currentNumber = duplicatesOrganised.get(i).getPhoneNumber();
            if(setToReturn.size()>0){
                for (int j = 0; j 

    The two model class which I use one is for getting the full contact list and the other is to get the duplictae number

    No 1 model Class

    public class ContactToDelete {
        private String number;
        private String name;
        private int repeatValue;
        private String contactID;
    
        public ContactToDelete(String number, String name, int repeatValue, String contactID) {
            this.number = number;
            this.name = name;
            this.repeatValue = repeatValue;
            this.contactID = contactID;
        }
    
        public ContactToDelete() {
        }
    
        public ContactToDelete(String number, String name, int repeatValue) {
            this.number = number;
            this.name = name;
            this.repeatValue = repeatValue;
        }
    
        public String getContactID() {
            return contactID;
        }
    
        public void setContactID(String contactID) {
            this.contactID = contactID;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getRepeatValue() {
            return repeatValue;
        }
    
        public void setRepeatValue(int repeatValue) {
            this.repeatValue = repeatValue;
        }
    }
    

    No 2 Model Class

    public class Contact {
        public String name;
        public String phoneNumber;
    
        public Contact(String name, String phoneNumber, String contactID) {
            this.name = name;
            this.phoneNumber = phoneNumber;
            this.contactID = contactID;
        }
        public String getContactID() {
            return contactID;
        }
    
        public void setContactID(String contactID) {
            this.contactID = contactID;
        }
        public String contactID;
        public Contact() {
        }
        public Contact(String name, String phoneNumber) {
            this.name = name;
            this.phoneNumber = phoneNumber;
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPhoneNumber() {
            return phoneNumber;
        }
    
        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    }
    

提交回复
热议问题