Android 游标适配器未在ListView中显示元素

Android 游标适配器未在ListView中显示元素,android,listview,simplecursoradapter,Android,Listview,Simplecursoradapter,我试图使用SimpleCursorColumn将SQLITE数据库的一列获取到listview。我在列表视图中获得了准确的行数,但是文本没有出现在列表中。我打印出光标的内容,它包含正确的元素 为什么文本没有显示在列表中?我在这件事上做傻事肯定很容易 public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { private stat

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

为什么文本没有显示在列表中?我在这件事上做傻事肯定很容易

        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);
    }
}
public类Tab3Fragment扩展片段实现LoaderManager.LoaderCallbacks{
私有静态最终整数加载器_ID=42;
专用游标适配器;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.tab3\u frag,容器,假);
ListView lv=view.findViewById(R.id.student\u视图);
_adapter=new SimpleCursorAdapter(getContext(),
R.layout.container\u list\u item\u视图,空,
新字符串[]{Contract.Student\u Table.STUDENTS\u COLUMN\u NAME},
新的int[]{R.id.list_item});
lv.设置适配器(_适配器);
getLoaderManager().initLoader(LOADER_ID,null,this);
返回视图;
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
if(id!=加载器\u id){
返回null;
}
返回新的游标装入器(getContext(),
Contract.Student\u Table.CONTENT\u URI,
新字符串[]{Contract.Student_Table.STUDENTS_COLUMN_ID,Contract.Student_Table.STUDENTS_COLUMN_NAME},null,null,
无效);
}
@凌驾
public void onLoadFinished(加载器、光标数据){
_适配器。swapCursor(数据);
}
@凌驾
公共void onLoaderReset(加载器){
_适配器.swapCursor(空);
}
}
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);
  }
}
公共类语言ListActivity扩展ListActivity实现
装载机回扣{
私有静态最终整数加载器_ID=42;
专用游标适配器;
创建时的公共void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.container_列表);
_适配器=新的SimpleCorsAdapter(此,
R.layout.container\u list\u item\u视图,空,
新字符串[]{DatabaseConstants.COL_LANG_NAME},
新的int[]{R.id.list_item});
setListAdapter(_适配器);
getLoaderManager().initLoader(LOADER_ID,null,this);
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
if(id!=加载器\u id){
返回null;
}
返回新的游标装入器(LanguageListActivity.this,
LanguageContentProvider.CONTENT\u URI,
新字符串[]{DatabaseConstants.COL_LANG_ID,DatabaseConstants.COL_LANG_NAME},null,null,
无效);
}
@凌驾
public void onLoadFinished(加载器、光标数据){
_适配器。swapCursor(数据);
}
@凌驾
公共void onLoaderReset(加载器){
_适配器.swapCursor(空);
}
}
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" />

容器列表项目视图.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>

更好地使用加载器。请尝试以下示例:

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);
  }
}
公共类语言ListActivity扩展ListActivity实现
装载机回扣{
私有静态最终整数加载器_ID=42;
专用游标适配器;
创建时的公共void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.container_列表);
_适配器=新的SimpleCorsAdapter(此,
R.layout.container\u list\u item\u视图,空,
新字符串[]{DatabaseConstants.COL_LANG_NAME},
新的int[]{R.id.list_item});
setListAdapter(_适配器);
getLoaderManager().initLoader(LOADER_ID,null,this);
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
if(id!=加载器\u id){
返回null;
}
返回新的游标装入器(LanguageListActivity.this,
LanguageContentProvider.CONTENT\u URI,
新字符串[]{DatabaseConstants.COL_LANG_ID,DatabaseConstants.COL_LANG_NAME},null,null,
无效);
}
@凌驾
public void onLoadFinished(加载器、光标数据){
_适配器。swapCursor(数据);
}
@凌驾
公共void onLoaderReset(加载器){
_适配器.swapCursor(空);
}
}
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" />

容器列表项目视图.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>


基于主题,有时textcolor可能与背景颜色相同,请尝试更改文本颜色。遗憾的是,事实并非如此。@我添加了xml文件,可能是一个明显的错误?基于主题,有时textcolor可能与背景颜色相同,尝试更改文本颜色。遗憾的是,不是这样。@我添加了xml文件,可能是一个明显的错误?鉴于我认为这是一个片段中的内容,大多数内容都不起作用,恐怕没有什么可担心的。将LoaderCallbacks实现到片段。然后开始编写代码。大多数不可用的方法都可以通过getContext()获取。如果您不想做这些更改。。为类似container_list_item_view.xml的项目创建一个新的xml文件,并将id student_视图放入textview,将其从listview所在的主布局中删除。。这也应该可以。由于片段的原因,我无法实现setListAdapter或在初始化加载程序时给出正确的回调。有什么解决办法吗?我已经研究了一些,但它们都需要特定于API的构建,这在我上面的评论中是错误的。我已经编辑过了。此外,在专栏中,只取一个值学生\专栏\姓名我更新了上面的帖子,我