2011-04-04 35 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