Fetch Contacts And Display In A Listview In Android

Here I am going to describe about how we can fetch all the contacts from our Android device and display them in a simple ListView. There will be some situations where we need to get all the contacts from our phone. Here I am explaining this in detail. We need to send query to the contentResolver and this will return a cursor to us. We need to pass a uri to the cursor to get the cursor. Cursor will point to a particular row. We can query like the following to get the contacts from Android.

  1. cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);     
This will return the cursor that contains all the contacts in the phone. We need to iterate and get the values by specifying the column name.
 
For creating the sample application, first create your layout file which will contains a ListView only like the following,
  1.  xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     xmlns:tools="http://schemas.android.com/tools"   
       
  3. <RelativeLayout>      

  4.  android:layout_width="match_parent"      
  5.       
  6.  android:layout_height="match_parent"      
  7.       
  8.  android:paddingBottom="@dimen/activity_vertical_margin"      
  9.       
  10.  android:paddingLeft="@dimen/activity_horizontal_margin"      
  11.       
  12.  android:paddingRight="@dimen/activity_horizontal_margin"      
  13.       
  14.  android:paddingTop="@dimen/activity_vertical_margin"      
  15.       
  16.  tools:context=".MainActivity">    
  17.     <ListView      
  18.       
  19.  android:id="@+id/listView"      
  20.       
  21.  android:layout_width="fill_parent"      
  22.       
  23.  android:layout_height="wrap_content" />    

  24. </RelativeLayout>     
Now in the java file bind this listview like the follwing,
  1. listView = (ListView) findViewById(R.id.listView);   
Now get the cursor by querying to the content resolver,
  1. cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);  
Here we will get the cursor, now we need to get only whatever the files we needed like contact display name, number, email id etc.
  1. String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};    
Now we need to specify the id of the elements in rows,
  1. int[] to = {android.R.id.text1, android.R.id.text2};   
Now we need to create the list adapter like the following,
  1. SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);    
Here we are using Android's built in adapter to the listview.
 
The last step is we need to set this adapter to the listview with setAdapter(),
  1. listView.setAdapter(adapter);    
Now run the app and all your contacts will be shown in the listview of your application.
 
Please see the screenshots,

. 

Up Next
    Ebook Download
    View all
    Learn
    View all