我创建了一个自定义联系人列表,但有些联系人被复制了。我在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;
}
这是Contact类
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);
}
}
答案 0 :(得分:0)
这是部分解决方案,更多是解决方法。您可以使用一组联系人而不是列表,即使用:
Set<Contact> tempContactHolder;
然后,当添加重复的联系人时,它实际上会被忽略。
如果您必须使用列表来存储联系人,因为您的代码在某些时候需要列表,然后要删除重复项,您可以将列表添加到集合中,这将删除重复项。然后,将设置内容添加回列表,即
Set<Contact> set = new HashSet<>();
set.addAll(tempContactHolder);
tempContactHolder.clear();
tempContactHolder.addAll(set);
最好的答案可能是弄清楚你的逻辑中是什么导致重复添加,并阻止它发生。
<强>更新强>
如果上述逻辑不起作用,一种可能的解释是您从未为equals()
类定义Contact
方法。在确定关于集合添加的重复项时,Java使用equals()
方法,因此这对于上述代码片段起作用至关重要。您可以尝试将以下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;
}