【发布时间】:2018-01-01 08:41:49
【问题描述】:
我一直在开发一个在列表中显示 contacts 的 Android 应用程序。我正在使用recyclerview,我可以在列表中显示联系人的姓名和图片。
但我的列表包含重复的联系电话。我使用下面的代码删除了重复项,但它显示了
9566191161
+919566191161 作为两个条目。 (即,)与国家代码一起显示为单独的条目。
我正在使用 POJO 类并将其作为列表添加到适配器。在 Pojo Class 中,我使用编码来删除重复项,例如
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stubs
if (obj instanceof ContactVO) {
ContactVO temp = (ContactVO) obj;
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());
}
}
我在 equals 方法中用作 "+91"+this.getContactNumber().contains(temp.getContactNumber()) 验证,但它不会删除该重复项。
你们能帮我解决这个错误吗?
我的代码sn-p
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);
contactVOList.add(new ContactVO(imageUri, name, phoneNumber));
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);
// contactVOList = removeDuplicates(contactVOList);
System.out.println("List size after removing duplicates =" + contactVOList.size());
System.out.println("ListSize before sendinf" + contactVOList.size());
mAdapter = new AllContactsAdapter(getContext(), contactVOList, userId, mobilenumber);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
rvContacts.setLayoutManager(mLayoutManager);
rvContacts.setItemAnimator(new DefaultItemAnimator());
rvContacts.setAdapter(mAdapter);
大家新年快乐!
【问题讨论】:
-
您可以将添加项目的位置的 sn-p 粘贴到列表中吗?
-
@SabidBinHabib 我已经添加了代码伙伴
-
您的问题与stackoverflow.com/q/47786280/819355 相同,在接受的答案中,您可以找到使用
Phone.CONTACT_ID以避免重复的代码
标签: java android android-contacts