请看一下: -
public static ArrayList<ContactsEntityBean> getContactDetails(
Context mContext) {
ArrayList<ContactsEntityBean> contactList = new ArrayList<ContactsEntityBean>();
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] {
id
}, null);
while (cur1.moveToNext()) {
ContactsEntityBean contactsEntityBean = new ContactsEntityBean();
// to get the contact names
String name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// Log.e("Name :", name);
String email = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// Log.e("Email", email);
contactsEntityBean.setName(name);
contactsEntityBean.setEmail(email);
if (email != null) {
contactList.add(contactsEntityBean);
}
}
cur1.close();
}
}
return contactList;
}
这个方法是从同一个用户返回多个联系人,假如我已经为同一个用户存储了abc @ gmail.com,abc @ gmail.com,那么它返回abc@gmail.com& abc@gmail.com 但我只想要一个记录abc@gmail.com
public static ArrayList<SearchEntityBean> getContactEmailDetails(
Context mContext) {
ArrayList<SearchEntityBean> contactList = new ArrayList<SearchEntityBean>();
try {
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String email = "";
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] {
id
}, null);
SearchEntityBean contactsEntityBean = new SearchEntityBean();
while (cur1.moveToNext()) {
// to get the contact names
String name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String image = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID));
String mail = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (mail != null) {
if (!mail.equalsIgnoreCase(LoginPreferenceClass
.getEmailID(mContext)))
email = email + mail + ",";
}
// Log.e("rohit", "Contact Email :" + email);
contactsEntityBean.setName(name);
contactsEntityBean.setImage(image);
}
if (email != null) {
if (email.length() > 0) {
if (email.split(",").length > 1) {
contactsEntityBean.setMutipleEmail(true);
}
contactsEntityBean.setUserType("2");
contactsEntityBean.setContactId(id);
contactsEntityBean.setEmail(email);
contactList.add(contactsEntityBean);
}
}
cur1.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
HashSet<SearchEntityBean> hs = new HashSet<SearchEntityBean>();
hs.addAll(contactList);
contactList.clear();
contactList.addAll(hs);
return contactList;
}
答案 0 :(得分:4)
您应该修改ContactsEntityBean
,如下所示
public class ContactsEntityBean {
private HashSet<String> emails = new HashSet<String>();
public void setEmail(String email) {
if (email == null)
return;
this.emails.add(email.trim());
}
public HashSet<String> getEmails() {
return this.emails;
}
}
将关注重复的电子邮件......您可以对地址,电话等使用相同的逻辑。
将您的ContactsEntityBean
替换为以下代码
public class ContactsEntityBean {
private HashSet<String> emails;
private HashSet<String> phones;
private HashSet<String> addresses;
private String contactId;
private boolean checked = false;
public ContactsEntityBean() {
this.emails = new HashSet<String>();
this.phones = new HashSet<String>();
this.addresses = new HashSet<String>();
}
public HashSet<String> getPhones() {
return phones;
}
public void setPhones(String phone) {
if (phone == null)
return;
this.phones.add(phone.trim());
}
public HashSet<String> getAddresses() {
return addresses;
}
public void setAddresses(String address) {
if (address == null)
return;
this.addresses.add(address.trim());
}
public void setEmails(String email) {
if (email == null)
return;
this.emails.add(email.trim());
}
public HashSet<String> getEmails() {
return emails;
}
public String getContactId() {
return contactId;
}
public void setContactId(String contactId) {
this.contactId = contactId;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
无需关心重复。这将关心所有的事情..
答案 1 :(得分:4)
这是我的工作
private void getContactDetails(ContentResolver contentResolver) {
Cursor phones = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
HashSet<String> mobileNoSet = new HashSet<String>();
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
String imagUri = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_URI));
long id = phones.getColumnIndex(ContactsContract.Contacts._ID);
if (!mobileNoSet.contains(phoneNumber)) {
arrayContacts.add(new Contact(name, phoneNumber, email,
imagUri, id));
mobileNoSet.add(phoneNumber);
}
}
adapterContact = new AdapterContact(getActivity(), arrayContacts);
listContact.setAdapter(adapterContact);
}
答案 2 :(得分:2)
通过改变你这样的方法你可以获得唯一一个不重复的联系人。
public static ArrayList<ContactsEntityBean> getContactDetails(Context mContext) {
ArrayList<ContactsEntityBean> contactList = new ArrayList<ContactsEntityBean>();
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
HashMap<String, String> data = new HashMap<String, String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (cur1.moveToNext()) {
ContactsEntityBean contactsEntityBean = new ContactsEntityBean();
// to get the contact names
String name = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// Log.e("Name :", name);
String email = cur1
.getString(cur1
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (!data.containsValue(email)) {
// Log.e("Email", email);
contactsEntityBean.setName(name);
contactsEntityBean.setEmail(email);
data.put(name, email);
if (email != null) {
contactList.add(contactsEntityBean);
}
}
}
cur1.close();
}
}
return contactList;
}
答案 3 :(得分:1)
将联系人保存在ArrayList 中,然后从列表中删除重复项。 删除重复元素的最简单方法是将内容添加到Set(不允许重复),然后将Set添加回ArrayList。
像这样使用HashSet
。
ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
答案 4 :(得分:0)
使用HashMap是因为HashMap不允许重复的键
声明全局变量
// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();
在循环中将数据添加到HashMap
// Enter Into Hash Map
nameEmailMap.put(email, name);
循环外获取日志
// Get The Contents of Hash Map in Log
for (Map.Entry<String, String> entry : namePhoneMap.entrySet()) {
String key = entry.getKey();
Log.d(TAG, "Phone :" + key);
String value = entry.getValue();
Log.d(TAG, "Name :" + value);
}
有关如何检索不重复的联系人的完整答案,请参阅https://stackoverflow.com/a/54227282/3904109(对于电子邮件)和https://stackoverflow.com/a/54228199/3904109(对于电话)
答案 5 :(得分:0)
此解决方案将:
删除重复项
删除所有不必要的开头,即当前仅允许数字以03、92和+92开头
显示所有同名电话,例如John有2个电话号码。
fun fetchContacts(): List<UserObject> {\
val contacts: ArrayList<UserObject> = ArrayList()
val mobileNoSet = HashSet<String>()
val cursor= requiredContext()?.contentResolver?.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI),null,null,"DISPLAY_NAME ASC")
while (cursor!!.moveToNext()) {
val index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
val photoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)
val strName = cursor.getString(index)
var number = cursor.getString(numberIndex)
number = number.replace(" ", "")
val photo = cursor.getString(photoIndex)
if ((number.startsWith("03") || number.startsWith("92")||number.startsWith("+92")) && !mobileNoSet.contains(number)) {
contacts.add(UserObject(photo,strName,number))mobileNoSet.add(number)
}
}
return contacts
}
答案 6 :(得分:-1)
在您的ArrayList中添加联系电子邮件后,在continue
之后添加HashSet
并使用if (email != null) {
contactList.add(contactsEntityBean);
continue;
}
而不是ArrayList
{{1}}