【发布时间】:2015-12-15 08:36:30
【问题描述】:
我正在从内容提供商处获取联系人,我需要在列表视图中显示它们。在此过程中,我成功获取了联系人,但它们包含重复值。现在 Phone.CONTACT_ID 对于每个联系人都是唯一的。我想根据该特定字段过滤我的联系人数组列表。
代码如下:
try {
cursor = getApplicationContext().getContentResolver()
.query(Phone.CONTENT_URI, null, null, null, null);
int Idx = cursor.getColumnIndex(Phone.CONTACT_ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_THUMBNAIL_URI);
cursor.moveToFirst();
do {
System.out.println("=====>in while");
HashMap<String, String> hashMap = new HashMap<String, String>();
contactid=cursor.getString(Idx);
name = cursor.getString(nameIdx);
phoneNumber = cursor.getString(phoneNumberIdx);
image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
hashMapsArrayList.add(hashMap);
}
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
在这里您可以看到我正在从光标中获取 4 个字段并将它们绑定到数组列表 hashMapsArrayList 即 ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>(); 。现在这个数组列表包含重复字段,我想根据 contactid 过滤它。
代码如下:
System.out.println("Original--->"+hashMapsArrayList);
for(int i=0;i<hashMapsArrayList.size();i++)
{
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = hashMapsArrayList.get(i);
String contactiddup=resultp.get("contactid");
if(!( hashMapsArrayListRemovedup.contains(contactiddup)))
{
System.out.println(hashMapsArrayListRemovedup);
System.out.println("In if added");
hashMapsArrayListRemovedup.add(resultp);
}else{
System.out.println("In else");
}
}
但上面的代码不起作用,新的arraylist也包含重复的值。
请帮忙。
示例输出重复:
12-15 14:10:57.217: I/System.out(8971): [{name=Didi America, image=null, phoneNumber=, contactid=7996}, {name=Didi America, image=null, phoneNumber=, contactid=7996}]
【问题讨论】:
-
将 ArrayList 元素添加到 LinkedHashSet 以删除重复元素
-
你的意思是什么重复值?提供者中没有重复数据
-
#kgandroid 请查看@Chirag 的回答
标签: android arraylist contacts