【发布时间】:2018-01-07 06:53:22
【问题描述】:
我正在使用联系人应用程序,我必须列出联系人数据库中的所有联系人,但它包含重复项。
例如,联系人号码 (98*******33, +9198********33) 被列为重复号码。
我使用过设置,在添加之前检查了我的列表是否包含电话号码,但这些都不起作用!
ContactVO 是 Pojo 类,contactVoList 是我要添加联系人的列表。
Cursor phones = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(" ", "");
imageUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("Name and Phone number = " + name + phoneNumber + imageUri);
if (contactVOList.size() == 0) {
contactVOList.add(new ContactVO(imageUri, name, phoneNumber, false));
} else {
if (!contactVOList.contains(phoneNumber)) {
contactVOList.add(new ContactVO(imageUri, name, phoneNumber, false));
}
}
System.out.println("List size before removing duplicates =" + contactVOList.size());
}
Set<ContactVO> s = new HashSet<ContactVO>();
s.addAll(contactVOList);
contactVOList = new ArrayList<ContactVO>();
contactVOList.addAll(s);
我的 Pojo 课
public class ContactVO {
private String ContactImage;
private String ContactName;
private String ContactNumber;
private int hashCode;
String id;
boolean clicked;
public ContactVO(String id) {
this.id = id;
}
public boolean isClicked() {
return clicked;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ContactVO(String ContactImage, String ContactName, String ContactNumber, Boolean clicked) {
this.ContactImage = ContactImage;
this.ContactName = ContactName;
this.ContactNumber = ContactNumber;
this.clicked = clicked;
}
public String getContactImage() {
return ContactImage;
}
public void setContactImage(String contactImage) {
this.ContactImage = ContactImage;
}
public String getContactName() {
return ContactName;
}
public void setContactName(String contactName) {
ContactName = contactName;
}
public String getContactNumber() {
return ContactNumber;
}
public void setContactNumber(String contactNumber) {
ContactNumber = contactNumber;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stubs
if (obj instanceof ContactVO) {
ContactVO temp = (ContactVO) obj;
System.out.println("this.getctno" + this.getContactNumber());
System.out.println("temp.getctno" + temp.getContactNumber());
if (this.getContactNumber() == temp.getContactNumber() && this.getContactName() == temp.getContactName() && (this.getContactNumber()).contains(temp.getContactNumber()))
return false;
}
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return (this.getContactNumber().hashCode() + this.getContactName().hashCode());
}
}
【问题讨论】:
-
您已经在使用
Set,这是解决方案。请查看 This thread 以了解 Set 的自定义对象实现。 -
@ADM 是的,但也不会删除重复项。
-
那是因为您使用的是自定义类。你已经覆盖了
Object方法。看看上面的解决方案。 -
@ADM,我已经编辑了我的问题并添加了 pojo 类。请您检查一次。我已经覆盖了 equals() 和 hashcode() 方法。
-
您的
equals方法包含多个错误。例如,您将字符串与==而非equals进行比较;如果另一个对象属于不相关的类,则返回true;似乎返回值通常是它应该是什么的否定,...