Loop does not catch duplicates and removes them in Android (Java)

I recently started programming in Android and Java, so please bear with me.

I wrote a loop that, before adding a new name and phone number to the list and the hidden array, delete all duplicates that it finds right before it. Using the current methods, I still get constant retries, and when you click the button to add all the same contacts again, I get all the contacts again. This makes me think that the duplicate verification method is not working correctly, but I am not getting any errors to help

I have two arrays of lists 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 a duplicate verification 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 is called after it has to check for duplicates and delete, the next method is the following number and name:

 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); } } 

I really lost what I could do wrong. I would try to clear this code, but it did not even work correctly for me to clear it.

+5
source share
2 answers

You can simply do this:

  • Create a bean class for human

    public class Person {private String name; private string telephone;

     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; } 

    }

  • Then drag and drop 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 Set } catch (Exception e) {e.printStackTrace (); }}

+3
source

I have no programming experience with android, but I see that you remove elements from phnnumber, while you iterate through phnnumbers, you should not do this. Either use an iterator, or add element / element indices to the list and remove them from phnnumbers after iterating through phnnumbers.

0
source

Source: https://habr.com/ru/post/1238320/


All Articles