how to display contacts in a listview in Android for Android api 11+ how to display contacts in a listview in Android for Android api 11+ android android

how to display contacts in a listview in Android for Android api 11+


Small stripped down example for displaying the contacts name in a ListView.Below Fragment extends ListFragment which has a default layout. You don't need to specify your own. The layout for list items is also taken from Android's default layouts (android.R.layout.simple_list_item_1) which is a simple single line of text per item.

import android.content.Context;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.provider.ContactsContract.Contacts;import android.support.v4.app.ListFragment;import android.support.v4.app.LoaderManager.LoaderCallbacks;import android.support.v4.content.CursorLoader;import android.support.v4.content.Loader;import android.support.v4.widget.CursorAdapter;import android.support.v4.widget.SimpleCursorAdapter;public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> {    private CursorAdapter mAdapter;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // create adapter once        Context context = getActivity();        int layout = android.R.layout.simple_list_item_1;        Cursor c = null; // there is no cursor yet        int flags = 0; // no auto-requery! Loader requeries.        mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);    }    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        // each time we are started use our listadapter        setListAdapter(mAdapter);        // and tell loader manager to start loading        getLoaderManager().initLoader(0, null, this);    }    // columns requested from the database    private static final String[] PROJECTION = {        Contacts._ID, // _ID is always required        Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display    };    // and name should be displayed in the text1 textview in item layout    private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };    private static final int[] TO = { android.R.id.text1 };    @Override    public Loader<Cursor> onCreateLoader(int id, Bundle args) {        // load from the "Contacts table"        Uri contentUri = Contacts.CONTENT_URI;        // no sub-selection, no sort order, simply every row        // projection says we want just the _id and the name column        return new CursorLoader(getActivity(),                contentUri,                PROJECTION,                null,                null,                null);    }    @Override    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {        // Once cursor is loaded, give it to adapter        mAdapter.swapCursor(data);    }    @Override    public void onLoaderReset(Loader<Cursor> loader) {        // on reset take any old cursor away        mAdapter.swapCursor(null);    }}