CursorAdapter 不显示 ListView 中的元素

CursorAdapter not displaying elements in ListView

我正在尝试使用 SimpleCursorColumn 将 SQLITE 数据库的一列添加到列表视图中。我在列表视图中得到准确的行数,但文本没有出现在列表中。我打印出光标的内容,它包含正确的元素。

为什么文本没有出现在列表中?一定很容易让我犯傻。

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

    private static final int LOADER_ID = 42;
    private CursorAdapter _adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3_frag, container, false);

        ListView lv = view.findViewById(R.id.student_view);
        _adapter = new SimpleCursorAdapter(getContext(),
                R.layout.container_list_item_view, null,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME},
                new int[] { R.id.list_item });
        lv.setAdapter(_adapter);
        getLoaderManager().initLoader(LOADER_ID, null, this);

        return view;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(getContext(),
                Contract.Student_Table.CONTENT_URI,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME}, null, null,
                null);
    }

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

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

tab3_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#c2f6ff"
    android:orientation="vertical">

    <ListView
        android:id="@+id/simple_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/student_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

</LinearLayout>

最好使用装载机。试试下面的例子:

public class LanguageListActivity extends ListActivity implements
        LoaderCallbacks<Cursor> {
    private static final int LOADER_ID = 42;
    private CursorAdapter _adapter;
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.container_list);
        _adapter = new SimpleCursorAdapter(this,
                R.layout.container_list_item_view, null,
                new String[] { DatabaseConstants.COL_LANG_NAME },
                new int[] { R.id.list_item });
        setListAdapter(_adapter);
        getLoaderManager().initLoader(LOADER_ID, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(LanguageListActivity.this,
                LanguageContentProvider.CONTENT_URI,
                new String[] { DatabaseConstants.COL_LANG_ID, DatabaseConstants.COL_LANG_NAME }, null, null,
                null);
    }

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

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

container_list.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_width="fill_parent"
    android:layout_height="fill_parent" />

container_list_item_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="2dip" >

    <TextView
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dip" />

</LinearLayout>