Java 从自定义联系人列表中删除重复联系人
我已经创建了一个自定义联系人列表,但有些联系人会被复制。我在SO中阅读了其他问题,但没有找到任何解决方案。 如何删除这些重复项 下面是列表的填充方式:Java 从自定义联系人列表中删除重复联系人,java,android,arraylist,duplicates,contacts,Java,Android,Arraylist,Duplicates,Contacts,我已经创建了一个自定义联系人列表,但有些联系人会被复制。我在SO中阅读了其他问题,但没有找到任何解决方案。 如何删除这些重复项 下面是列表的填充方式: protected Void doInBackground(String[] filters) { String filter = filters[0]; ContentResolver contentResolver = context.getContentResolver(); Uri uri
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);
}
}
公共类联系人实现可包裹{
公共字符串id、姓名、电话、标签;
联系人(字符串id、字符串名称、字符串电话、字符串标签){
这个.id=id;
this.name=name;
这个。电话=电话;
这个标签=标签;
}
受保护触点(包裹输入){
id=in.readString();
name=in.readString();
phone=in.readString();
label=in.readString();
}
公共静态最终创建者=新创建者(){
@凌驾
公共联系人createFromParcel(包裹中){
返回新联系人(in);
}
@凌驾
公共联系人[]新数组(整数大小){
返回新联系人[大小];
}
};
@凌驾
公共字符串toString()
{
返回姓名+“|”+标签+”:“+电话;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入限制(id);
目的地书面记录(名称);
目的地记录(电话);
目的地记录(标签);
}
}
这是一个局部解决方案,更多的是一种变通方法。您可以使用一组联系人而不是列表,即使用:
Set<Contact> tempContactHolder;
哪里定义了
tempContactHolder
?它是在doInBackgroundcheck之外定义的,用于检查重复的联系人并删除,但不起作用。是否有其他方法创建带有复选框的自定义联系人列表?这不起作用
。。。如果我的解决方案无效,请更新您的问题并向我们展示联系人
课程。也许您的equals()
逻辑导致了意外的行为。
Set<Contact> set = new HashSet<>();
set.addAll(tempContactHolder);
tempContactHolder.clear();
tempContactHolder.addAll(set);
@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;
}