Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get distinct contacts in Android

I am successfully storing contacts in parse.com dashboard data browser by this code.

public void readContacts(){
         ContentResolver cr = getContentResolver();
         Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

         if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) ==1) {
                    System.out.println(name );
                    ParseObject testObject = new ParseObject("Contacts");

                    testObject.put("names", name);

                    // get the phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                           new String[]{id}, null);
                    while (pCur.moveToNext()) {
                          String phone = pCur.getString(
                                 pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         System.out.println( phone);
                        testObject.put("phonenumber", phone);

                    }
                    pCur.close();
                    testObject.saveInBackground();
           }
        }
     }
  }

But there is no check for the duplicate contacts !

It stores all the contacts duplicate from sim / phone memory.

How can it be avoided ?

One possible method I think is to store distinct names(contact) in local database, & then retrieving that data to store it in parse.com

Is there exists a better way ?

Thanks in advance...

like image 293
Vivek Warde Avatar asked Sep 12 '14 05:09

Vivek Warde


Video Answer


3 Answers

Please see the below method. You will get contacts list which does not have duplicate phone numbers.

 public void readContacts() {
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        ArrayList<ParseObject> contacts = new ArrayList<ParseObject>();
        ArrayList<String> list = new ArrayList<String>();
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) == 1) {
                    System.out.println(name);
                    ParseObject testObject = new ParseObject("Contacts");

                    testObject.put("names", name);

                    // get the phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                    while (pCur.moveToNext()) {
                        String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        System.out.println(phone);
                        testObject.put("phonenumber", phone);
                        if(!list.contains(phone)) {
                            contacts.add(testObject);
                        }

                        list.add(phone);

                    }

                    pCur.close();
                    testObject.saveInBackground();
                }
            }
        }
    }
like image 41
Sunny Avatar answered Oct 06 '22 23:10

Sunny


An easy approach could be to load the data to a MatrixCursor with no duplicate data. For example lets assume you have a cursor c1 will many contacts, but you need a cursor with no duplicate data. Here is what you could do:

MatrixCursor mc = new MatrixCursor(new String[] { 
                        Phone._ID, 
                        Phone.DISPLAY_NAME_PRIMARY,
                        Phone.NUMBER
});

String lastNumber = "";

while(c1.moveToNext()){
    String id = c1.getString(c1.getColumnIndexOrThrow(Phone._ID));
    String name = c1.getString(c1.getColumnIndexOrThrow(Phone.DISPLAY_NAME_PRIMARY)));
    String number = c1.getString(c1.getColumnIndexOrThrow(Phone.NUMBER));

    //Some condition to check previous data is not matched and only then add row
    if(!lastNumber.contains(number)){
            lastNumber = number;
            mc.addRow(new String[]{id, name, number});
    }


}

c1.close();

Make an instance of MatrixCursor with same columns, and then load if last number or contact name does not match that of the previous contact. The condition for checking is upto you. Query data in some order so that the duplicate contacts stay together first.

Once the MatrixCursor is loaded you can fetch data from it. You could also attach it to a view through a custom CursorLoader or CursorAdapter.

like image 142
ZakiMak Avatar answered Oct 07 '22 01:10

ZakiMak


Set is a collection in java that does not allow duplicates. You can put your data into a set with number as a key and name as value, to avoid duplicate numbers.

And later you can take them back from set and put into your testObject with name as key and number as value.

like image 1
Suneesh Avatar answered Oct 06 '22 23:10

Suneesh