kga*_*oid 5 android arraylist contacts
我从内容提供商处获取联系人,我需要在列表视图中显示它们.在此过程中,我成功获取联系人,但它们包含重复的值.现在每个联系人都没有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();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到我从光标中取出4个字段并将它们绑定到arraylist hashMapsArrayList中 ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>();.现在这个arraylist包含重复的字段,我想根据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");
}
}
Run Code Online (Sandbox Code Playgroud)
但上面的代码不起作用,新的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}]
Run Code Online (Sandbox Code Playgroud)
当您在 while 循环中构建列表时,您可以并且应该删除重复项。只需使用 aHashSet来收集唯一的 id,并且仅将具有新标识符的条目添加到 List 中:
Set<String> ids = new HashSet<>();
do {
System.out.println("=====>in while");
contactid=cursor.getString(Idx);
if (!ids.contains(contactid)) {
ids.add(contactid);
HashMap<String, String> hashMap = new HashMap<String, String>();
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());
Run Code Online (Sandbox Code Playgroud)
您不需要第二个代码片段(我没有检查其中有什么问题)。
| 归档时间: |
|
| 查看次数: |
910 次 |
| 最近记录: |