5

I am fetching contacts from content provider and I need to display them in a listview.In the process I successfully fetch the contacts,but they contains duplicate values.Now Phone.CONTACT_ID is unique for each contact.I want to filter my arraylist of contacts based on that particular 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 fetching 4 fields from the cursor and binding them into the arraylist hashMapsArrayList which is ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>(); .Now this arraylist contains duplicate fields and I want to filter it based on the 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 above code is not working and the new arraylist also contains duplicate values.

Please help.

A sample output duplicacy:

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}]
kgandroid
  • 5,507
  • 5
  • 39
  • 69

3 Answers3

3

You can, and should remove the duplicates when you construct the List, in the while loop. Just use a HashSet to collect the unique ids, and only add entries having new a identifier to the 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());

You don't need the second code snippet (I didn't check to see what's wrong in it).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Out of curosity can you please have a look at the 2nd code snippet and tell me what wrong I was doing?? – kgandroid Dec 15 '15 at 09:28
  • @kgandroid It looks like you only add unique ids to `hashMapsArrayListRemovedup`. You are not removing duplicates from `hashMapsArrayList`. – Eran Dec 15 '15 at 09:33
  • Yes I was doing that...I was taking a new arraylist and copying unique elements to it...But it was not working...all values were adding in the new arraylist – kgandroid Dec 15 '15 at 09:34
  • @Eran I have been trying to get this functionality for weeks. With your help, it's finally over. – CHarris Apr 17 '16 at 22:06
1

Use HashSet to remove duplicate entries.

Eg.

public static HashSet<String> hSet= new HashSet<String>();

Add value to HasSet using hSet.add(yourString);

For get value from HashSet you have to use Iterator

Iterator iterator = hSet.iterator(); 
while(iterator.hasNext()){
        String str = iterator.next();
}
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
0

Alternatively you can check for frequency as remove as well. refer this code.it may help you.

Collections.frequency(arrayList, number)

public static void main(String[] args) {
    ArrayList<Integer> arrayList = new ArrayList<Integer>();
    arrayList.add(5);
    arrayList.add(1);
    arrayList.add(5);
    arrayList.add(1);
    arrayList.add(5);
    arrayList.add(2);
    arrayList.add(3);

    ArrayList<Integer> unique = new ArrayList<>();

    for (Integer number : arrayList) {
        if (Collections.frequency(arrayList, number) == 1) {
            unique.add(number);
        }
    }

    System.out.println(unique);
}
JIGAR
  • 302
  • 2
  • 15