Search code examples
androidcontactsandroid-contacts

how many contacts in contact list


How can I tell how many contacts there are in the contact list? I got the contact number, but one person can have more than one contact and I want to account for this in finding the total number of contacts in the contact list.


Solution

  • To find the count of phone numbers of all the contacts

    Cursor cursor =  managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    
    int count = cursor.getCount();
    

    To find the count of all the phone numbers of a particular RawContactID (pass the contact id value in rawContactId).

    Cursor cursor =  managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + " = " + rawContactId, null, null);
    
        int count = cursor.getCount();
    

    The number of contacts displayed in the ContactsListActivity consists can be determined by following query.

    Cursor cursor =  managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    

    int count = cursor.getCount();

    However if a person has been entered under multiple accounts then only a single instance is obtained by the above query as ContactsContract.Contacts combines all such contacts.

    Cursor cursor =  managedQuery(RawContacts.CONTENT_URI, null, null, null, null);
    

    int count = cursor.getCount();

    The relation between ContactsContract.Contacts and RawContacts can be found out at http://developer.android.com/resources/articles/contacts.html

    Hope this resolves your doubt!