我已经将我的手机联系人排序并列入了一个arraylist但是,我在列表中找到了许多相同联系人姓名的副本。这种情况怎么样?怎么避免这个?
这是我试过的,
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
"(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
while (cursor.moveToNext()) {
try {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact_names_list.add(name);
phone_num_list.add(phonenumber);
} catch (Exception e) {
e.printStackTrace();
}
谁能帮忙?
这里似乎没有人回答你的问题。
您看到重复联系人的原因是您要查询电话而不是联系人。
在Android中有3个主要表:
Contacts
表 - 每个联系人有一个项目RawContacts
表 - 每个帐户有一个项目每个联系人(如谷歌,Outlook,Whatsapp等) - 多个RawContacts
链接到单个Contact
Data
表 - 每个细节有一个项目(姓名,电子邮件,电话,地址等) - 每个数据项链接到一个RawContact
,并且多个Data
行链接到每个RawContact
。您正在查询CommonDataKinds.Phone.CONTENT_URI
这是Data
表的一部分,所以如果联系人有多个电话,和/或它有来自多个来源的同一部电话(例如Google和Whatsapp),您将获得相同的电话相同的CONTACT_ID
多一次。
解决方案是,使用HashMap
(而不是HashSet
),其中键是CONTACT_ID
,因此您可以显示每个联系人的多个电话:
String[] projection = new String[] { CommonDataKinds.Phone.CONTACT_ID, CommonDataKinds.Phone.DISPLAY_NAME, CommonDataKinds.Phone.NUMBER };
Cursor cursor = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
HashMap<Long, Contact> contacts = new HashMap<>();
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
Contact c = contacts.get(id);
if (c == null) {
// newly found contact, add to Map
c = new Contact();
c.name = name;
contacts.put(id, c);
}
// add phone to contact class
c.phones.add(phone);
}
cursor.close();
// simple class to store multiple phones per contact
private class Contact {
public String name;
// use can use a HashSet here to avoid duplicate phones per contact
public List<String> phones = new ArrayList<>();
}
如果要按名称对HashMap进行排序:
List<Contact> values = new ArrayList<>(contacts.values());
Collections.sort(values, new Comparator<Contact> {
public int compare(Contact a, Contact b) {
return a.name.compareTo(b.name);
}
});
// iterate the sorted list, per contact:
for (Contact contact : values) {
Log.i(TAG, "contact " + contact.name + ": ");
// iterate the list of phones within each contact:
for (String phone : contact.phones) {
Log.i(TAG, "\t phone: " + phone);
}
}
你可以试试HashSet
。
公共类HashSet扩展AbstractSet实现Set,Cloneable,Serializable
代码结构
HashSet<String> hashSET = new HashSet<String>();
hashSET.add("AA");
hashSET.add("BB");
hashSET.add("CC");
hashSET.add("AA"); // Adding duplicate elements
然后
Iterator<String> j = hashSET.iterator();
while (j.hasNext())
System.out.println(j.next()); // Will print "AA" once.
}
现在SORT
使用Hashset
你的TreeSet
值。
TreeSet实现SortedSet接口,因此不允许重复值。
TreeSet<String> _treeSET= new TreeSet<String>(hashSET);
可能在您的联系人中有多个群组,该群组将是WhatsApp,Google等。转到您的联系人并搜索具有whatsApp帐户的联系人。将显示与不同组的双重输入
你应该使用或更改你的ContactsBean
,在你的Bean
使用HashSet
注意:
HashSet
可以避免重复输入more
HashSet
只包含独特的元素,它可以避免HashSet
相同的关键元素
示例bean
public class ContactBean {
private HashSet<String> number = new HashSet<String>();
public void setNumber(String number) {
if (number == null)
return;
this.number.add(number.trim());
}
public HashSet<String> getNumber() {
return this.number;
}
}
简单的例子
//Creating HashSet and adding elements
HashSet<String> hashSet=new HashSet<String>();
hashSet.add("Dhruv");
hashSet.add("Akash");
hashSet.add("Dhruv"); //Avoiding this entry
hashSet.add("Nirmal");
//Traversing elements
Iterator<String> itr = hashSet.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
您可以使用HashSet
来避免重复: -
HashSet<String> hset =
new HashSet<String>();
你可以在ArrayList
中添加像HashSet
: -
hset.add(your_string);
要么
转换你的ArrayList
到HashSet
: -
Set<String> set = new HashSet<String>(your_arraylist_object);
HashSet
避免重复输入:)
我不知道你为什么从联系人那里得到重复的项目,也许手机联系人已经有重复的值。你可以在通讯录应用程序中查看。 您应始终使用设置数据结构,以避免重复项目。你可以找到更好的解释和示例here。
我认为你的重复是由于Whatsapp联系人干扰了联系。所以你可以使用这样的东西
String lastPhoneName = "";
String lastPhoneNumber = "";
//IN YOUR CONTACT FETCHING WHILE LOOP , INSIDE TRY
String contactName = c.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phNumber = c
.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (!contactName.equalsIgnoreCase(lastPhoneName) && !phNumber.equalsIgnoreCase(lastPhoneNumber)) {
lastPhoneName = contactName;
lastPhoneNumber = phNumber;
ContactModel model = new ContactModel();
model.setContact_id(contactid);
model.setContact_name(contactName.replaceAll("\\s+", ""));
model.setContact_number(phNumber.replaceAll("\\D+", ""));
list_contact_model.add(model);
}
这将检查前一个号码是否与旧号码相同而不是跳过它。我希望你得到答案
HashSet
在键/值对中添加项目,并从项目集中删除重复的条目。
List<String> phone_num_list= new ArrayList<>();
// add elements to phone_num_list, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(phone_num_list);
phone_num_list.clear();
phone_num_list.addAll(hs);
快乐的编码!!