How to show phone contacts in ListView

Here is my code, in fact on the screen it does not show me any contact. 5 contacts added to the emulator. 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); } 
+3
android listview contacts
Mar 02 2018-11-11T00
source share
2 answers

I 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); } 

Check if you did something wrong with the textview implementation?

+5
Mar 08 '12 at 10:33
source share

At first, I would just narrow down the problem.

1) Check if you have permission to read contacts

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

2) Check if the cursor has any results

 cur.getCount() 
+1
Mar 02 2018-11-11T00:
source share



All Articles