2010-09-23 51 views
0

是否可以根據來自自定義遊標適配器的行ID填充列表視圖。我不斷收到一個錯誤「illegalArguementException列:_id不存在」。但數據庫有它,並且已被正確使用。我不知道該怎麼做,因爲我需要從同一個數據庫填充不同的列表視圖,而且我不想創建多個數據庫,這些數據庫仍然具有相同的列名稱。任何想法將不勝感激。感謝可能從遊標行ID填充列表視圖?

這裏是數據庫代碼正在感冒:

public Cursor retrieveItemRow(long Id){ 
     open(); 
String[] Columns = new String[] {TITLE,DATE, TIME}; 
Cursor row = db.query(true,DATABASE_TABLE, Columns, KEY_ID +"=" +Id, null, null, null, null,null); 
    if(row != null){ 
    row.moveToNext(); 
    return row; 
    } 
    return row; 
     } 

這裏有一個方法,我想它調用:

public void fillRowData(long Id) { 

Cursor cursor = adapter.retrieveRow(id); 
startManagingCursor(cursor); 
String[] from = new String[]{DBAdapter.TITLE, DBAdapter.DATE, DBAdapter.TIME}; 
int[] to = new int[]{R.id.Name, R.id.Date, R.id.Time}; 

customCursor items = new customCursor(this, R.layout.viewlist, cursor, from, to); 
setListAdapter(items);   

} 

回答

0

確保您有_id中所表示的SELECT語句通過光標你可以進入你的適配器。例如,如果我使用的是SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to),那麼我的Cursor c將以「SELECT _id,name FROM users;」之類的東西爲起點。

Notepad Tutorial

private void fillData() { 
    // Get all of the notes from the database and create the item list 
    Cursor c = mDbHelper.fetchAllNotes(); 
    startManagingCursor(c); 

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE }; 
    int[] to = new int[] { R.id.text1 }; 

    // Now create an array adapter and set it to display using our row 
    SimpleCursorAdapter notes = 
      new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); 
    setListAdapter(notes); 
} 

教程限定在返回所有Note對象的數據庫輔助類的方法。基本上他們使用SQLiteDatabasequery方法返回一個Cursor對象。查詢的選擇字符串是您應該包含_id列的位置。

因此,對於您的情況,您需要將KEY_ID添加到您的Columns字符串數組變量中,因爲這些是包含在您的select語句(db.query())中的列。這也暗示在CursorAdapter文檔中:

光標必須包含一個名爲列 「_id」或本級將無法正常工作。

+0

對不起打擾,但我有點困惑。是否在調用遊標的數據庫類或適配器中包含select語句?請任何鏈接到這個例子,將不勝感激。 – Rexx 2010-09-23 17:33:46

+0

我剛剛更新了我的初始回覆,請檢查並讓我知道這是否合理。 – McStretch 2010-09-23 17:46:38

+0

我已經有一個從列表視圖填充的列表視圖。我現在想要做的是用我已經獲得的rowI填充另一個listview。像Cursor c = mdbHelper.fetchNote(rowId)。這是我得到錯誤的地方。 – Rexx 2010-09-23 18:20:56