Search This Blog

Tuesday 3 July 2012

Using the Contact Provider to Display the Contact List


Contact Provider Sample


Contact Provider provides the access to the contact database and allow the apps to add,delete,modify and query the contact database. This post is focusing on displaying the list of contacts to a list view.


Create a android project in eclipse and open the main.xml from the res->layout->main.xml.


Add a list view to the xml layout file

<ListView android:layout_width="match_parent"
              android:id="@+id/contactList"
              android:layout_height="wrap_content"
              android:layout_weight="1"/>

Add a new xml layout file named as contact_info.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/contactEntryText"
        android:layout_gravity="fill_horizontal"
        android:text="@+id/contactEntryText" />
</GridLayout>


Now open the activity class and add the following code.

public class ContactSampleActivity extends Activity {
    /** Called when the activity is first created. */
    private ListView mContactList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContactList = (ListView) findViewById(R.id.contactlist);
        showContactList();
    }
    
    private void showContactList() {
        // Build adapter with contact entries
        Cursor cursor = getContacts();
        String[] fields = new String[] {
                ContactsContract.Data.DISPLAY_NAME
        };
        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_info, cursor,
                fields, new int[] {R.id.contactEntryText});
        mContactList.setAdapter(adapter);
    }

    /**
     * Obtains the contact list for the currently selected account.
     *
     * @return A cursor for for accessing the contact list.
     */
    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + "1" + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

}

By running this sample you will get the list of all the contacts with their display name. You need to test this on your phone to see the list.






No comments:

Post a Comment