【问题标题】:Deleting duplicate contacts from custom contact list从自定义联系人列表中删除重复的联系人
【发布时间】:2017-07-18 04:06:15
【问题描述】:

我创建了一个自定义联系人列表,但有些联系人重复了。我已经阅读了 SO 中的其他问题,但没有得到任何解决方案。 如何删除这些重复项?

这是列表的填充方式:

protected Void doInBackground(String[] filters) {
        String filter = filters[0];
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[]{
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
        };
        Cursor cursor;
        if(filter.length()>0) {
            cursor = contentResolver.query(
                    uri,
                    projection,
                    ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
                    new String[]{filter},
                    ContactsContract.Contacts.DISPLAY_NAME + " ASC"
            );
        }else {
            cursor = contentResolver.query(
                    uri,
                    projection,
                    null,
                    null,
                    ContactsContract.Contacts.DISPLAY_NAME + " ASC"
            );
        }
        totalContactsCount = cursor.getCount();
        if(cursor!=null && cursor.getCount()>0){
            while(cursor.moveToNext()) {
                if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                    String id = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts.DISPLAY_NAME));

                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
                            new String[]{id},
                            null
                    );

                    if (phoneCursor != null && phoneCursor.getCount() > 0) {
                        while (phoneCursor.moveToNext()) {
                            String phId = phoneCursor.getString(phoneCursor.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone._ID));

                            String customLabel = phoneCursor.getString(phoneCursor.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.LABEL));

                            String label = (String) ContactsContract.CommonDataKinds.Phone
                                    .getTypeLabel(context.getResources(),
                                    phoneCursor.getInt(phoneCursor.getColumnIndex(
                                            ContactsContract.CommonDataKinds.Phone.TYPE)),
                                    customLabel
                            );
                            String phNo = phoneCursor.getString(phoneCursor.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER));

                            tempContactHolder.add(new Contact(phId, name, phNo, label));
                        }
                        phoneCursor.close();
                    }
                }
                loadedContactsCount++;
                publishProgress();
            }
            cursor.close();
        }
        return null;
    }

这是联系人类

public class Contact implements Parcelable {

    public String id,name,phone,label;

    Contact(String id, String name,String phone,String label){
        this.id=id;
        this.name=name;
        this.phone=phone;
        this.label=label;
    }

    protected Contact(Parcel in) {
        id = in.readString();
        name = in.readString();
        phone = in.readString();
        label = in.readString();
    }

    public static final Creator<Contact> CREATOR = new Creator<Contact>() {
        @Override
        public Contact createFromParcel(Parcel in) {
            return new Contact(in);
        }

        @Override
        public Contact[] newArray(int size) {
            return new Contact[size];
        }
    };

    @Override
    public String toString()
    {
        return name+" | "+label+" : "+phone;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(name);
        dest.writeString(phone);
        dest.writeString(label);
    }
}

【问题讨论】:

  • tempContactHolder 定义在哪里?
  • 定义在doInBackground之外
  • 检查重复的联系人并使用this删除

标签: java android arraylist duplicates contacts


【解决方案1】:

这是部分解决方案,更多的是一种解决方法。您可以使用一组联系人而不是列表,即使用:

Set<Contact> tempContactHolder;

然后,当添加重复的联系人时,它实际上会被忽略。

如果您必须使用列表来存储联系人,因为您的代码在某些时候需要列表,那么要删除重复项,您可以将列表添加到集合中,这将删除重复项。然后,将设置的内容添加回您的列表,即

Set<Contact> set = new HashSet<>();
set.addAll(tempContactHolder);
tempContactHolder.clear();
tempContactHolder.addAll(set);

最好的答案可能是找出您的逻辑中导致添加重复项的原因,并阻止这种情况发生。

更新:

如果上述逻辑不起作用,一种可能的解释是您从未为您的Contact 类定义equals() 方法。 Java 使用equals() 方法来确定与添加到集合有关的重复项,因此这对于上述代码 sn-p 的工作至关重要。您可以尝试将以下 equals() 方法添加到您的 Contact 类中:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (!Contact.class.isAssignableFrom(obj.getClass())) {
        return false;
    }
    final Contact other = (Contact) obj;
    if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
        return false;
    }
    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
        return false;
    }
    if ((this.phone == null) ? (other.phone != null) : !this.phone.equals(other.phone)) {
        return false;
    }
    if ((this.label == null) ? (other.label != null) : !this.label.equals(other.label)) {
        return false;
    }
    return true;
}

【讨论】:

  • 这不起作用。还有其他方法可以创建带有复选框的自定义联系人列表吗?
  • This doesn't work ... 我的解决方案不起作用怎么办?
  • 更新您的问题并向我们展示Contact 课程。也许您的 equals() 逻辑导致了意外行为。
猜你喜欢
  • 2013-11-18
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2014-02-14
  • 2017-02-03
  • 2021-02-08
  • 1970-01-01
  • 2015-12-21
相关资源
最近更新 更多