简单游标适配器不工作
我正在使用数据库,从中获取光标,然后使用简单的光标适配器来填充列表视图。我似乎无法弄清楚为什么应用程序在创建新的 simplecursoradapter 时崩溃。我的光标包含在一个单例对象中。我通过此类中的数据变量引用了它。
String[] columns = new String[] {"item", "quantity", "days"};
int[] to = new int[] { R.id.nametext, R.id.quantitytext, R.id.dayslefttext};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.itemrow, data.listCursor, columns, to);
listView1.setAdapter(sCA);
simplecursoradapter 如何处理在其构造函数中传递给它的信息?参数之一的格式是否错误?这是每行的简单 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="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/nametext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/quantitytext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/dayslefttext"/>
</LinearLayout>
这是我加载游标的方式:
listCursor = myDataBase.query(ITEMS_TABLE, null, "location" +" = ?", new String[]{IN_SPECIAL_LIST}, null, null, "item");
我无法弄清楚问题是什么。我通过一些调试运行了这个,当我创建新的 simplecursoradapter 时出现问题。
I am using a database, getting a cursor out of it and then using a simplecursoradapter to populate a listview. I can't seem to figure out why the app crashes on creating a new simplecursoradapter. My cursor is contained in a singleton object. I have a reference to it through the data variable in this class.
String[] columns = new String[] {"item", "quantity", "days"};
int[] to = new int[] { R.id.nametext, R.id.quantitytext, R.id.dayslefttext};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.itemrow, data.listCursor, columns, to);
listView1.setAdapter(sCA);
What does the simplecursoradapter do with the information passed to it in its constructor? Is one of the parameters in the wrong format? Here is the simple xml file for each row that I am using to just see if this works:
<?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="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/nametext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/quantitytext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/dayslefttext"/>
</LinearLayout>
Here is how I load the cursors:
listCursor = myDataBase.query(ITEMS_TABLE, null, "location" +" = ?", new String[]{IN_SPECIAL_LIST}, null, null, "item");
I can't figure out what the problems is. I ran this through some debugging and the problem occurs when I I create the new simplecursoradapter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个 null 是你的问题,这意味着查询将使用 SELECT * ,但表中没有列 _id ,所以你应该使用
SimpleCursorAdapter 需要 _id 列
YOURID 可以是 ROWID ( ROWID as _id) 或数据库中的任何 int 或 long ID
this null is your problem, it means that query will use
SELECT *
but there is no column_id
in your table so you should useSimpleCursorAdapter need _id column
YOURID could be ROWID (ROWID as _id) or any int or long ID from your DB