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.