我正在尝试制作联系人列表视图片段。这是代码:
public class ContactsListFragment extends RoboListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int CONTACTS_LIST_LOADER = 0x01;
CursorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate", "called");
String[] fromColumns = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.ADDRESS};
int[] toViews = { R.id.contact_list_item_id,
R.id.contact_list_item_name,
R.id.contact_list_item_email};
getLoaderManager().initLoader(CONTACTS_LIST_LOADER, null, this);
adapter = new SimpleCursorAdapter(
getActivity().getApplicationContext(), R.layout.contacts_list_item,
null, fromColumns, toViews,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("onCreateView", "called");
View rootView = inflater.inflate(R.layout.contacts_list_fragment, container, false);
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d("onCreateLoader", "called");
return new CursorLoader(getActivity(),
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Log.d("onLoadFinished", "called");
adapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d("onLoaderReset", "called");
adapter.swapCursor(null);
}
}
但是虽然打印了所有日志,并且没有例外,但我在屏幕上看不到任何内容。
片段布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.Fragments.ContactsListFragment">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/contact_list_empty"/>
</RelativeLayout>
列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/contact_list_item_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/contact_list_item_name"
android:layout_below="@id/contact_list_item_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/contact_list_item_email"
android:layout_below="@id/contact_list_item_name"/>
</RelativeLayout>
我在这里有点不知所措。我的父活动包括片段:
<fragment
android:id="@+id/contact_list_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.Fragments.ContactsListFragment"
tools:layout="@layout/contacts_list_fragment"
android:layout_below="@id/fakeView"/>