java - 循环不捕获重复项并在 Android(Java) 中删除它们

标签 java android loops arraylist duplicates

我最近开始使用 Android 和 Java 编程,所以请多多包涵。

我写了一个循环,在将新的姓名和电话号码添加到列表和隐藏数组之前,应该删除之前找到的所有重复项。使用当前方法,我仍然会不断重复,当再次单击按钮添加所有相同的联系人时,我会再次获得所有联系人。这让我觉得重复检查方法根本无法正常工作,但我没有得到任何帮助的错误

我有两个在外部创建的列表数组:

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

这是添加联系人的方法:

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

这是重复检查方法:

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

这是在检查重复项并删除后调用的方法,下一个方法是下一个添加数字和名称的方法:

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

我真的不知道我可能做错了什么。我会尝试清理这段代码,但它甚至无法正常工作,我无法清理它。

最佳答案

简单地说,你可以这样做:

  1. 为person创建一个bean类

    公共(public)类人物{ 私有(private)字符串名称; 私有(private)字符串电话;

    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. 然后遍历所有的联系人,将他们放在一个集合中,它会自动避免重复

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

    //在这里你可以用 Person's Set 做什么 } 捕获(异常e){ e.printStackTrace(); }

关于java - 循环不捕获重复项并在 Android(Java) 中删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301880/

相关文章:

PHP数组循环+MySQL数据

java - 只接受字符、数字和特殊字符的正则表达式

java - for 进入线程的进度对话框

java - 使用监听器捕获 Spring Batch 中步骤中的错误

android - 防止 Dagger 2 组件在屏幕上旋转

android - 我如何停止 firebase debugView

java - POI - Java - 使用 for 循环更新单元格

变量/范围的 Swift Looping 持久性

java - 将图像从数据库加载到 Spring 应用程序上下文中。我使用mybatis api

java - 应用程序关闭时调用方法