Question

I am working on a project where I need to list down all the contacts. I am following this article.

My AndroidManifest.xml contains below, so i can read the contacts:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Code is here:

private void getContacts() {
    try {
        // Form an array specifying which columns to return. 
        String[] projection = new String[] {
                                    People._ID,
                                    People._COUNT,
                                    People.NAME,
                                    People.NUMBER};
        
        // Get the base URI for the People table in the Contacts content provider.
        Uri contacts =  People.CONTENT_URI;
        
        // Make the query. 
        Cursor managedCursor = managedQuery(contacts,
                                    projection, // Which columns to return 
                                    null,       // Which rows to return (all rows)
                                    null,       // Selection arguments (none)
                                    // Put the results in ascending order by name
                                    People.NAME + " ASC");
                
        printContacts(managedCursor);
    }
    catch(Exception ex) {
        Log.d("Contacts",ex.toString());
    }
}

private void printContacts(Cursor cur){ 
    if (cur.moveToFirst()) {
        String name; 
        String phoneNumber; 
        int nameColumn = cur.getColumnIndex(People.NAME); 
        int phoneColumn = cur.getColumnIndex(People.NUMBER);
        String imagePath; 
    
        do {
            // Get the field values
            name = cur.getString(nameColumn);
            phoneNumber = cur.getString(phoneColumn);
            Log.d("Contacts","Name: "+ name + " **** Phone: "+ phoneNumber);
        } while (cur.moveToNext());
    }
}

When I run it on the Emulator(2.3.3) it throws below error:

java.lang.IllegalArgumentException: Invalid column _count

Can someone fix it? Great thanks for your valuable time & help.

Was it helpful?

Solution

If you remove the string People._COUNT everything works

See also: Android SDK - List All Users

OTHER TIPS

Android People class is deprecated. You should use ContactsContract instead. Since you run it on Emulator API level 10, and the class is deprecated since API level 5, there's no reason to keep using People.

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