Amazon Gift Cards

Thursday, December 25, 2008

Display Contact Names in Android

There are a lot of tutorial on this but most of them either failed when you run the codes or they forgat to tell you something, while i don't guaranty anything coz it took me 3 hours to figure what's wrong with what i was doing, i would try to make it as simple as i can.

First of all when you need to read Contacts on android, you need the user's permission that you will read their contact list. To do this
* Open AndroidManifest.xml and add the following codes in between manifest tags
<uses-permission android:name="android.permission.READ_CONTACTS">

- On eclipse, open AndroidManifest.xml then go to Permissions Tab, Click Add, Click on "Uses Permission", on the right side, on the name field select android.permission.READ_CONTACTS

After adding the permission, let us go to the codes: On your codes,
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        Cursor contactsCursor = this.managedQuery(People.CONTENT_URI, null, null, null, null);

        String[] columnsToMap = new String[] {People.NAME};
        int[] mapTo = new int[] {};

        ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, contactsCursor, columnsToMap, mapTo);

A brief explanation to the core codes:
Cursor contactsCursor = this.managedQuery(People.CONTENT_URI, null, null, null, null);
-- This would query our Contacts, you can treat this somehow like SQL statement, we query the People.CONTENT_URI model, you can find out what the null represents in here

-- Tell it to start managing this cursor

String[] columnsToMap = new String[] {People.NAME};
-- As stated, you can treat this as sql, what we just did here is that we put the field People.NAME to an Array of String, you may have many fields as possible in this array. Here you can see we use People.NAME instead of a string field name, well this is the same except the class People has a static variable named NAME which translate to the string field name. The whole list of possible fields are in here

int[] mapTo = new int[] {};
-- This part is confusing but to explain it, we would map the People.NAME field to a textbox, now where did we specify the textbox?

ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, contactsCursor, columnsToMap, mapTo);
-- SimpleCursorAdaptor would bind/map our Cursor with our layout, you can refer here on what the parameters represent. The 2nd parameter here is layout, android by default has a lot of layout given to you by default, one of the layout that it has is the simple_list_item_1 layout, the 2nd comment on this thread would help you understand why we need the textbox mentioned on the top. The 3rd parameter is our cursor, the fourth is the fields/columns we need from our cursor to be mapped to our fifth parameters.

Hope i didn't confuse you and hope it helps :)

Update History
   Jan 17, 2012 - Visual Update


Bryan said...

Do you have the XML that you used for the listview, and the XML for each row item? I am looking for formatting ideas on how to make the listview presentable.

Also, I noticed that in testing my program, that if on of the columns I am displaying in the list contains a fairly long text, the remaining columns are not shown. How can I add a horizontal scroll?


- Bryan

admin said...

bryan, you can try to refer to the documentation There is a topic there on how to format it on a custom way.

admin said...

bryan, sry seems the documentation is not updated. Ill create a post on how to customize the listview when I got home from my vacation. Tnx

Pete said...

Does anyone know why my new contacts do not show up alphabetically anymore on my droid? The only way to find them is to search them and I need to spell them right to find them.

Eugene van der Merwe said...

You just saved my life. I have been following this tutorial

on custom listviews but it's way to complicated for what I wanted to achieve. Yours is the most clear and concise tutorial on how to have custom database fields in a listview. In my case I wanted to convert "2011-11-30 05:44:12" to "30 Nov 05:44" and this tutorial did just the job. Thank you.

Anonymous said...


the Contacts.People api is deprected for quite some time now. The new approch using the ContactsContract was introduced already at api level 5. The suggested way to get information on Contacts is now using the ContactsContract.RawContacts and ContactsContract.Contacts content provider. For more information on that topic you can take a look at