Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay Contacts List using Fragment
    primarykey
    data
    text
    <p>I want to display Contact List using Fragment,</p> <p>I download sample from here <a href="https://github.com/codinguser/android_contact_picker" rel="nofollow">contact_picker</a></p> <p>In this sample they use ListFragment, I have change it to Fragment and all other necessory changes then I run the application but it doesn't show the contact list, how can I done it with Fragment</p> <p><strong>Edited:</strong></p> <p>I just Changed in below tow files</p> <p>ContactsListFragment.java</p> <pre><code>package com.codinguser.android.contactpicker; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.widget.SimpleCursorAdapter; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AlphabetIndexer; import android.widget.ListView; import android.widget.SectionIndexer; public class ContactsListFragment extends Fragment implements LoaderCallbacks&lt;Cursor&gt;{ private OnContactSelectedListener mContactsListener; private SimpleCursorAdapter mAdapter; private String mCurrentFilter = null; ListView listView; String TAG = getClass().getSimpleName(); private static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.HAS_PHONE_NUMBER, Contacts.LOOKUP_KEY }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_contacts_list, container, false); listView = (ListView) view.findViewById(R.id.list); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); getLoaderManager().initLoader(0, null, this); mAdapter = new IndexedListAdapter( this.getActivity(), R.layout.list_item_contacts, null, new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.display_name}); listView.setAdapter(mAdapter); listView.setFastScrollEnabled(true); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mContactsListener = (OnContactSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnContactSelectedListener"); } } @Override public Loader&lt;Cursor&gt; onCreateLoader(int arg0, Bundle arg1) { Uri baseUri; if (mCurrentFilter != null) { baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurrentFilter)); } else { baseUri = Contacts.CONTENT_URI; } String selection = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; Log.v(TAG, "Name = "+ContactsContract.Contacts.DISPLAY_NAME); return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION, selection, null, sortOrder); } @Override public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) { mAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader&lt;Cursor&gt; loader) { mAdapter.swapCursor(null); } class IndexedListAdapter extends SimpleCursorAdapter implements SectionIndexer{ AlphabetIndexer alphaIndexer; public IndexedListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to, 0); } @Override public Cursor swapCursor(Cursor c) { if (c != null) { alphaIndexer = new AlphabetIndexer(c, c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ"); Log.v(TAG, "data = "+ c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); } return super.swapCursor(c); } @Override public int getPositionForSection(int section) { return alphaIndexer.getPositionForSection(section); } @Override public int getSectionForPosition(int position) { return alphaIndexer.getSectionForPosition(position); } @Override public Object[] getSections() { return alphaIndexer == null ? null : alphaIndexer.getSections(); } } } </code></pre> <p>fragment_contacts_list.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:drawSelectorOnTop="false" /&gt; &lt;TextView android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:text="Nothing to see here!" android:textAppearance="?android:attr/textAppearanceLarge" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload