【发布时间】: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