我嘗試執行以下代碼指數走出與SQLite數據庫綁定異常
在activity.java文件
DatabaseEvent mDbHelper = new DatabaseEvent(getApplicationContext());
mDbHelper.open();
Cursor notesCursor = mDbHelper.fetchEvent();
startManagingCursor(notesCursor);
String[] from = new String[]{DatabaseEvent.KEY_ETITLE, DatabaseEvent.KEY_DISTANCE, DatabaseEvent.KEY_IMGNAME, DatabaseEvent.KEY_DESCRIPTION, DatabaseEvent.KEY_EID};
int[] to = new int[]{R.id.title, R.id.duration, R.id.list_image, R.id.artist, R.id.id};
SimpleCursorAdapter event =
new SimpleCursorAdapter(getApplicationContext(), R.layout.list_row, notesCursor, from, to);
在DatabaseEvent.java
public long createEvent(String title, String distance, String imgname, String description, String eid) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ETITLE, title);
initialValues.put(KEY_DISTANCE, distance);
initialValues.put(KEY_IMGNAME, imgname);
initialValues.put(KEY_DESCRIPTION, description);
initialValues.put(KEY_EID, eid);
Log.v("INFO1","inserting db");
return mDb.insert(EVENT_TABLE, null, initialValues);
}
public Cursor fetchEvent() {
Log.v("INFO1","fetching db");
Cursor mCursor = mDb.query(EVENT_TABLE, new String[] {KEY_ROWID, KEY_ETITLE, KEY_DISTANCE,
KEY_IMGNAME, KEY_DESCRIPTION, KEY_EID}, null, null,null,null, KEY_DISTANCE+" ASC");
return mCursor;
}
在logcat的,我可以清楚地看到日誌消息「插入數據庫」打印三次意味着數據日期真的添加到數據庫中,但日誌消息「fetching db」打印一次並給我流動錯誤說:
CursorIndexOutOfBoundException index -1, requsted, with a size of 60,
我嘗試了不同的功能像moveToFirst()和使用MoveToNext(),但還是沒能解決問題,任何一個可以給我的手,任何幫助將greately感激!
請發表您的所有logcat的錯誤。我認爲這個錯誤並不是你認爲的那樣...... – Sam
當然,提取部分只顯示一次,你只查詢一次數據庫。 – Barak