title: how to display contacts in a listview in Android for Android api 11+
body: <p>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 <code>managedQuery</code> and other deprecated solutions...</p>
  
  <p>I went through the <a href="http://developer.android.com/training/contacts-provider/retrieve-names.html" rel="noreferrer">android developer training for retrieving a contact list</a>, 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.)</p>
  
  <p>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.</p>
  
  <p>I have followed the tutorial to the best of my knowledge by no contacts show up. I think the biggest thing is that the <code>TO_IDS</code> is an integer array that points to <code>android.R.id.text1</code>. I'm confused how that is supposed to somehow pull an array of contact names. </p>
  
  <p>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 <code>R.layout.contact_list_item</code> which is just textviews populated by TO_IDS, an array of integers.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>What am I doing wrong and how do I simply display the contact list in a listview? </p>
  
  <p>EDIT: adding in my code:</p>
  
  <p>in my fragment class:</p>
  
  <pre><code>public class MyFragment extends Fragment implements
      LoaderManager.LoaderCallbacks&lt;Cursor&gt;{
  
  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&lt;Cursor&gt; onCreateLoader(int i, Bundle bundle) {
      return null;
  }
  
  @Override
  public void onLoadFinished(Loader&lt;Cursor&gt; cursorLoader, Cursor cursor) {
  
  }
  
  @Override
  public void onLoaderReset(Loader&lt;Cursor&gt; cursorLoader) {
  
  }
  }
  </code></pre>
  
  <p>in my activity_main.xml:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;fragment
              android:id ="@+id/contactListFragment"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:name="preamble.myapp.MyFragment"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>in my contact_list_view xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"/&gt;
  </code></pre>
  
  <p>In my contact_list_item xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"/&gt;
  </code></pre>
  
  <p>finally for contact_list_layout xml:</p>
  
  <p>What do I put in the <code>contact_list_layout.xml</code>? Is this just an empty <code>&lt;LinearLayout&gt;</code>? 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 <code>listview</code> already in the <code>contact_list_view.xml</code>? </p>