带有ContentProvider的SimpleCursorAdapter; ListView为空

时间:2015-02-18 14:04:02

标签: java android android-layout listview simplecursoradapter

我正在使用SimpleCursorAdapter尝试显示来自内容提供商的数据。但ListView保持空白。一直在寻找3天的答案。

这是我的代码:

IssuesFragment.java

public class IssuesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

private SimpleCursorAdapter issuesAdapter;
private static final int ISSUES_LOADER = 0;

private static final String[] ISSUES_COLUMNS = {
        DbContract.issues._ID,
        DbContract.issues.ID,
        DbContract.issues.THUMBNAIL,
        DbContract.issues.TEXT
};


public static final int COL_ISSUES__ID = 0;
public static final int COL_ISSUES_ID = 1;
public static final int COL_ISSUES_THUMBNAIL = 2;
public static final int COL_ISSUES_TEXT = 3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public IssuesFragment() {
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    getLoaderManager().initLoader(ISSUES_LOADER, null, this);
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_issues, container, false);
    ListView listView = (ListView) rootView.findViewById(R.id.issues_listview);
    getLoaderManager().initLoader(ISSUES_LOADER, null, this);

    issuesAdapter = new SimpleCursorAdapter (
                    getActivity(),
                    R.layout.issues_list_item,
                    null,
                    ISSUES_COLUMNS,
                    new int[]{
                            R.id.issues_list__id,
                            R.id.issues_list_id,
                            R.id.issues_list_thumbnail,
                            R.id.issues_list_text,
                    },
                    0
            );
    listView.setAdapter(issuesAdapter);
    return rootView;
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    Uri issuesEntry = DbContract.issues.buildIssuesUri();

    return new CursorLoader(
            getActivity(),
            issuesEntry,
            ISSUES_COLUMNS,
            null,
            null,
            null
    );
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    issuesAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    issuesAdapter.swapCursor(null);
}

}

以下是相应的布局文件:

issues_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:padding="16dp">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/issues_list__id" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/issues_list_id" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/issues_list_thumbnail" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/issues_list_text" />
</LinearLayout>

fragment_issues.xml

    <LinearLayout 
     ....
    />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/issues_listview"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/emptyListElem"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="Keine Ausgaben vorhanden." />

</LinearLayout>

我的光标充满了数据。我在DatabaseUtils.dumpCursorToString(cursor)方法中打印onLoadFinished验证了该问题。但是数据不会从那里传到UI。

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。来自fragment_issues.xml的元素emptyListElem Textview正在替换list元素,即使我有

View empty = getActivity().findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);

在我的 onCreateView 方法中,只有当没有内容填充列表时,AFAIK才会显示emptyListElem。好像我错了。或许我把代码放在了错误的地方。

编辑:我删除了元素emptyListElem,现在可以正常工作。