微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Loop没有捕获重复项并在Android(Java)中删除它们

我最近开始使用Android和Java编程,所以请耐心等待.

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

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

List

这是添加联系人方法

 public void AddAllContacts(View view) {
    try {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,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

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

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

我真的迷失了我可能做错的事.我会尝试清理这些代码,但它甚至无法正常工作以便我清理它.

最佳答案
你可以这样做:

>为person创建bean类

公共类人{
    私有字符串名称;
    私人手机;

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

}
>然后迭代所有的联系人并将它们放在一个集合中,它将自动避免dulicates

Setnes = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,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做什么
        }
        catch(例外e){
            e.printstacktrace();
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐