使用片段显示联系人列表

时间:2013-06-18 05:54:35

标签: android android-fragments

我想使用Fragment显示联系人列表,

我从这里下载样本contact_picker

在这个示例中,他们使用ListFragment,我已将其更改为Fragment和所有其他必需的更改然后我运行应用程序但它没有显示联系人列表,我怎么能用Fragment

来完成它

编辑:

我刚刚在以下两个文件中进行了更改

ContactsListFragment.java

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<Cursor>{

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<Cursor> 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<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> 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();
    }
}
}

fragment_contacts_list.xml

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

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<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" />

</LinearLayout>

0 个答案:

没有答案