2011-04-04 74 views
2

我正在使用数据库,将光标移出,然后使用simplecursoradapter填充列表视图。我似乎无法弄清楚为什么应用程序崩溃创建一个新的simplecursoradapter。我的光标包含在一个单独的对象中。我通过这个类中的数据变量来引用它。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时会出现问题。

+0

您是否看过Logcat输出?抛出什么异常? – 2011-04-04 10:35:36

回答

5
myDataBase.query(ITEMS_TABLE, null, ... 

这个空是你的问题,这意味着查询将使用SELECT *但没有列_id在你的表,所以你应该使用

new String[] {"YOURID AS _id", "item", "quantity", "days"} 

SimpleCursorAdapter需要_id列

YOURID可能是ROWID(ROWID为_id)或来自您的数据库的任何int或long ID

+0

好吧,现在正在工作,谢谢!但是我无法将所有数据都放到列表行中。我只显示来自第一个textview的文本。 – 2011-04-04 23:06:37

+0

@Flawed_Logicc你能发布你的itemrow.xml布局吗? – Selvin 2011-04-05 13:11:57

+0

我得到它的工作,我无意中重叠Textviews。谢谢! – 2011-04-06 04:06:45