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

Terence Chow picture Terence Chow · Aug 13, 2013 · Viewed 23.4k times · Source

I'm sorry if this looks like the same question a million times...but a google search for this provides no results, just a bunch of outdated tutorials using managedQuery and other deprecated solutions...

I went through the android developer training for retrieving a contact list, but the tutorial is incomplete and even downloading the sample code doesn't help because the sample code is for more advanced contact list manipulation (search, etc.)

In any case, there is no reason why there should not be a simple solution to this so I'm hoping someone can answer here because I'm sure this has been done a million times and I'm sure dozens of other beginning android developers would appreciate this.

I have followed the tutorial to the best of my knowledge by no contacts show up. I think the biggest thing is that the TO_IDS is an integer array that points to android.R.id.text1. I'm confused how that is supposed to somehow pull an array of contact names.

Also, I'm confused why a textview is needed when the end goal is to display a listview...In the tutorial, we have mContactsList which is a list view...But we populate the list view with an adapter pointing to R.layout.contact_list_item which is just textviews populated by TO_IDS, an array of integers.

mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view);
mCursorAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.contact_list_item,
            null,
            FROM_COLUMNS, TO_IDS,
            0);
mContactList.setAdapter(mCursorAdapter);

What am I doing wrong and how do I simply display the contact list in a listview?

EDIT: adding in my code:

in my fragment class:

public class MyFragment extends Fragment implements
    LoaderManager.LoaderCallbacks<Cursor>{

private static final String[] FROM_COLUMNS = {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO_IDS = {android.R.id.text1};
ListView mContactList;
private SimpleCursorAdapter mCursorAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.contact_list_view,container,false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view);
    mCursorAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.contact_list_item,
            null,
            FROM_COLUMNS, TO_IDS,
            0);
    mContactList.setAdapter(mCursorAdapter);

}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    return null;
}

@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {

}

@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {

}
}

in my activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
            android:id ="@+id/contactListFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:name="preamble.myapp.MyFragment"/>
</LinearLayout>

in my contact_list_view xml :

<?xml version="1.0" encoding="utf-8"?>
<ListView   xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/list"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>

In my contact_list_item xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

finally for contact_list_layout xml:

What do I put in the contact_list_layout.xml? Is this just an empty <LinearLayout>? It's not clear in the tutorial how this xml is handled. It says this XML is the fragment, but if it's the fragment, why did we define a listview already in the contact_list_view.xml?

Answer

zapl picture zapl · Aug 13, 2013

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