Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


how to display contacts in a listview in android for android api 11 using -'java,android,listview,simplecursoradapter'


how to display contacts in a listview in android for android api 11  using -'java,android,listview,simplecursoradapter'

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?
    

asked Sep 7, 2015 by rajesh
0 votes
13 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...