Akshat Vajpayee
Akshat Vajpayee

Reputation: 324

Getting Null Contact id when querying with account name in Raw Contacts in Android

   private void displayAllContactsByType(String accountName) {
        Cursor rawCursor = null;

        try {
            rawCursor = mCProviderClient.query(
                    ContactsContract.RawContacts.CONTENT_URI,
                    null,
                    ContactsContract.RawContacts.ACCOUNT_NAME + "= ?",
                    new String[]{accountName},
                null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        int contactIdColumn = rawCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);


        Utils.Log("Raw Size", " " + rawCursor.getCount());

        while (rawCursor.moveToNext()) {


            String contactId = rawCursor.getString(contactIdColumn);

           // Utils.Log("contactId ",contactId);
            storeContactDetails(contactId);
        }

        if (contactList != null) {


            contactListener = (ContactListener) context;
            contactListener.updateView(contactList);

        }

    }

I am getting all the account name which raw contacts contain. And I would like to get all the contacts belongs to the particular account name but I am getting null contact id. How can I get all the contact id to a particular account name and then respective data of that contact id along with it?

Upvotes: 0

Views: 390

Answers (1)

marmor
marmor

Reputation: 28179

First, RawContacts.CONTACT_ID is of type Long, not String, so you should do:

Long contactId = rawCursor.getLong(contactIdColumn);

See: https://developer.android.com/reference/android/provider/ContactsContract.RawContactsColumns.html#CONTACT_ID don't pay attention to the type of the CONTACT_ID field which are always Strings, look at Type: INTEGER below it.

Second, this is rare, but can happen when a RawContact does not belong to any Contact, I call those zombie RawContacts, they can be created either via a corrupt DB or a bug, or intentionally using AGGREGATION_MODE when creating the RawContact, see AGGREGATION_MODE_DISABLED

Upvotes: 3

Related Questions