3

Following is my code, actually on screen it's not showing me any contact. In emulator I have 5 contacts added. Please tell me what to do.

{
    //some code
    Cursor cur = getContacts();
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
    SimpleCursorAdapter adapter = 
        new SimpleCursorAdapter(this, 
                                R.layout.list_view_item_new,
                                cur,
                                fields,
                                new int[] {R.id.contactEntryText});
    lv.setAdapter(adapter);
}

private Cursor getContacts() {  
    // Run query     
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = 
        new String[]{ ContactsContract.Contacts._ID,
                      ContactsContract.Contacts.DISPLAY_NAME }; 
    String selection = null;
    String[] selectionArgs = null;  
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
        " COLLATE LOCALIZED ASC";  
    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);  
}
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Neha
  • 187
  • 3
  • 5
  • 15
  • Could you show us what list_view_item_new looks like? – E.Z. Hart Mar 02 '11 at 15:03
  • thanks for pointing in that direction actually i was using linear layout inside that i have textview. now i have deleted linear layout . i am getting my list. – Neha Mar 03 '11 at 09:07

2 Answers2

5

I have copied and executed almost the same code and it works:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cur = getContacts();

        ListView lv = getListView();

       String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};

       SimpleCursorAdapter adapter = 
                new SimpleCursorAdapter(this, 
                                        R.layout.main,
                                        cur,
                                        fields,
                                        new int[] {R.id.txtbox});
          lv.setAdapter(adapter);         
    }    

    private Cursor getContacts() {  
        // Run query     
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = 
                new String[]{ ContactsContract.Contacts._ID,
                              ContactsContract.Contacts.DISPLAY_NAME }; 
            String selection = null;
            String[] selectionArgs = null;  
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
                " COLLATE LOCALIZED ASC";  
            return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

Please check if you have done anything wrong in the textview implementation?

adi
  • 66
  • 1
  • 2
  • For those who want also to have a projection on the phone number. I tried this example but it was giving me an error saying invalid column data1. Here's the solution: http://stackoverflow.com/questions/21811780/android-contact-phone-number-using-loader-invalid-column-data1 – JPerk May 15 '17 at 00:40
1

First I would just narrow the problem.

1) Check if you have permissions for reading contacts

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

2) Check if cursor have any results

cur.getCount()
wonglik
  • 1,043
  • 4
  • 18
  • 36
  • 1
    androidmanifest file have this permission and getcount() returns 5 but still i am not getting values in adapter. Is there any way to check row data in cursor?? if in debuging we can check, then please tell me which property to check?? thanks in advance.. – Neha Mar 02 '11 at 10:58
  • @neha you can always iterate through cursor like : 'while (c.moveToNext())...' but one things that bother me. Once you use 'ContactsContract.Data.DISPLAY_NAME' and once 'ContactsContract.Contacts.DISPLAY_NAME'. Are those same? – wonglik Mar 02 '11 at 11:35