0

I am able to get total number of contacts but the problem is, its coming from the sqlite and the number of contacts in the phonebook is different from the number I am getting from the database. Here's my code for that:

Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        // Or this cursor
        Cursor cursor = managedQuery(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);

        int count = cursor.getCount();

        try {
            tv1.setText(String.valueOf(count));
            Log.i("Contacts: ", String.valueOf(count));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I am able to get the total number of contacts in the database but its different from the contacts displayed in the phonebook.

Any suggestions or help will be really appreciated.

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • What you get might be correct, because in contacts app, it will not count the joined contacts!!! – Harish Talanki Jan 30 '14 at 14:59
  • Thanks for the info. Is there any alternative way to get the exact info though ? – mike20132013 Jan 30 '14 at 15:03
  • I guess, its something to do with the 4 null parameters that you are passing. Try different methods in [this](http://stackoverflow.com/questions/1721279/how-to-read-contacts-on-android-2-0) link. – Harish Talanki Jan 30 '14 at 15:20

1 Answers1

1

I'm still learning so I'm sure there's a faster way than this, but what I did to only count unique names was use an if statement to get the unique names to assign to my contact list and count the number of unique names, which gave me an accurate number. Here's the code I use, I hope somebody can take it and advise maybe a more efficient way. Hope this helps.

"nameCount" is the number you're looking for in this example.

private void getContacts() {

        String name = "";
        String contact_id;
        int nameCount = 0;

        uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
        selection = null;
        selectionArgs = null;
        sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;

        Cursor peopleCursor = getContentResolver().query(uri, projection,
                selection, selectionArgs, sortOrder);

        if (peopleCursor != null) {

            int indexName = peopleCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexId = peopleCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

            while (peopleCursor.moveToNext()) {

                // this is a separate activity used for my listView.  
                //It containts 2 strings (name and _id) with getters and setters for both
                ContactNameItems nameItems = new ContactNameItems();

                // Filter out no-name contacts such as auto-added email addresses from gmail
                // Compare to value of 'name' to see if they're the same, if so then pass
                if (!peopleCursor.getString(indexName).isEmpty() &&
                            !peopleCursor.getString(indexName)
                                    .equalsIgnoreCase(name) {

                    name = peopleCursor.getString(indexName);
                    contact_id = peopleCursor.getString(indexId);
                    nameCount++; //Do something with this


                    nameItems.setName(name);
                    nameItems.set_id(contact_id);

                    //Listview to add my 'nameItems'
                    mListView.add(nameItems);

                    mListAdapter.notifyDataSetChanged();
                }
            }
            peopleCursor.close();
        }
    }
Psest328
  • 6,575
  • 11
  • 55
  • 90