Question

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.

Was it helpful?

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!

OTHER TIPS

A really old thread, but if you want to count contacts WITH phone numbers you can use this:

Cursor cursor =  managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.HAS_PHONE_NUMBER, null, null);
int count = cursor.getCount();

Of course managedQuery is deprecated now, but this can help in a bind :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top