I get contacts from a content provider and I need to display them in a list. In the process, I successfully retrieve the contacts, but they contain duplicate values .Now Phone.CONTACT_ID is unique for each contact. I want to filter my arraylist of contacts based on this specific field.
Here is the code:
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(); } }
Here you can see that I am extracting 4 fields from the cursor and bind them to arraylist hashMapsArrayList , which is equal to ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>(); . Now this arraylist contains duplicate fields, and I want to filter it based on contactid .
Here is the code:
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"); } }
But the code above does not work, and the new arraylist also contains duplicate values.
Please, help.
Example duplication of output:
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}]