Moh
Moh

Reputation: 149

Loop is not catching duplicates and removing them in Android(Java)

I've recently started programming in Android and Java in general so please bear with me.

I wrote a loop that should, before adding a new name and phone number to a list and hidden array, remove any duplicates it finds right before. Using the current methods I still get constant repeats, and when clicking the button to add all the same contacts again, I get all the contacts again. This makes me think the duplicate check method isn't working correctly at all, but Im not getting any erors to help

I have two list arrays that I created outside:

List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();

This is the Adding contacts method:

 public void AddAllContacts(View view) {
    try {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while (phones.moveToNext()) {
            String linesp = System.getProperty("line.separator");
            TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            duplicatecheck(name, phoneNumber);
            addthistothelist(name, phoneNumber);
        }
        phones.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

This is the duplicate check method:

public void duplicatecheck(String name,String phoneNumber) {
    for (int i=0;i<phnnumbers.size();i++) {
        String thenumber = phnnumbers.get(i);
        String thename= names.get(i);
    if(thenumber.equals(phoneNumber)) {
            phnnumbers.remove(i);
            names.remove(i);
        TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
        String textpost = quantityTextView.getText().toString();
        String newtextpost = textpost.replaceAll(thenumber, "UNBELIEVABLEEE");
        String secondtextpost = newtextpost.replaceAll(thename, "UNBELIEVABLE");
        quantityTextView.setText(secondtextpost);
        NumberOfContactsAdded--;
        }
    }
}

This is the method that gets called after it should check for duplicates and remove, this next method is the one adding the number and name next:

public void addthistothelist(String nameofperson,String NumberOfPerson) {

    String linesp = System.getProperty("line.separator");
    TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
    String textpost = quantityTextView.getText().toString();
    NumberOfPerson = NumberOfPerson.replaceAll("[^0-9]", "");
    if(NumberOfPerson.contains("+1")) {
        phnnumbers.add(NumberOfPerson);
        names.add(nameofperson);
        NumberOfContactsAdded++;
        quantityTextView.append(linesp+nameofperson+" " +NumberOfPerson);
    } else  {
        NumberOfPerson= "+1"+NumberOfPerson;
        phnnumbers.add(NumberOfPerson);
        names.add(nameofperson);
        NumberOfContactsAdded++;
        quantityTextView.append(linesp+nameofperson+" " +NumberOfPerson);
    }
}

Im really lost on what I could be doing wrong. I would attempt to clean up this code but its not even working properly for me to clean it up.

Upvotes: 5

Views: 247

Answers (2)

Bahramdun Adil
Bahramdun Adil

Reputation: 6079

Simply you can do like this:

  1. Create a bean class for person

    public class Person { private String name; private String phone;

    public Person(String name, String phone) {
        this.name = name;
        phone = phone.replaceAll("\\W+", "");
        phone = "+1"+phone;
        this.phone = phone;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Person person = (Person) o;
        return name != null ? name.equals(person.name) : person.name == null && (phone != null ? phone.equals(person.phone) : person.phone == null);
    
    }
    
    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (phone != null ? phone.hashCode() : 0);
        return result;
    }
    

    }

  2. Then iterate all the contacts and put them in a set, it will automatically avoid dulicates

    Set<Person> persons = new HashSet<>();
    
    public void AddAllContacts(View view) {
        try {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    
        while (phones.moveToNext()) {
            String linesp = System.getProperty("line.separator");
            TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Person person = new Person(name, phoneNumber);
            persons.add(person);
        }
        phones.close();
    

    // here what you want you can do with Person's Set } catch (Exception e){ e.printStackTrace(); } }

Upvotes: 3

nullwarrior
nullwarrior

Reputation: 1

I don't have experience of programming with android, but one thing I can see is that you are removing elements from phnnumbers whilst you are iterating through phnnumbers, you should not do this. Either use an iterator, or add the elements/element indexes to a list, and remove them from phnnumbers after iterating through phnnumbers.

Upvotes: 0

Related Questions