2011-10-01 81 views
0

當我使用cursor.moveToNext();在我的代碼然後應用程序強制整理活動當我向下滾動底部,但是當我刪除cursor.moveToNext();它正常工作?光標代碼給出錯誤?

class NoteHolder { 
private Button b1 = null; 
private Button b2 = null; 
private Button b3 = null; 


NoteHolder(View row) { 
    b1 = (Button) row.findViewById(R.id.one); 
    b2 = (Button) row.findViewById(R.id.two); 
    b3 = (Button) row.findViewById(R.id.three); 

} 

void populateFrom(Cursor c, NoteHelper helper) { 
    b1.setText(helper.getNote(c)); 
     c.moveToNext(); 
    b2.setText(helper.getNote(c)); 
    c.moveToNext(); 
    b3.setText(helper.getNote(c)); 
} 

}

錯誤是

ERROR/AndroidRuntime(622): FATAL EXCEPTION: main 
ERROR/AndroidRuntime(622): android.database.CursorIndexOutOfBoundsException: Index 16 requested, with a size of 16 
ERROR/AndroidRuntime(622):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
ERROR/AndroidRuntime(622):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
ERROR/AndroidRuntime(622):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
ERROR/AndroidRuntime(622):  at com.example.buttontest.ButtonTest$NoteHelper.getNote(ButtonTest.java:141) 
ERROR/AndroidRuntime(622):  at com.example.buttontest.ButtonTest$NoteHolder.populateFrom(ButtonTest.java:105) 
ERROR/AndroidRuntime(622):  at com.example.buttontest.ButtonTest$NoteAdapter.bindView(ButtonTest.java:71) 
ERROR/AndroidRuntime(622):  at android.widget.CursorAdapter.getView(CursorAdapter.java:186) 
ERROR/AndroidRuntime(622):  at android.widget.AbsListView.obtainView(AbsListView.java:1294) 
ERROR/AndroidRuntime(622):  at android.widget.ListView.makeAndAddView(ListView.java:1727) 
ERROR/AndroidRuntime(622):  at android.widget.ListView.fillDown(ListView.java:652) 
ERROR/AndroidRuntime(622):  at android.widget.ListView.fillGap(ListView.java:623) 
ERROR/AndroidRuntime(622):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944) 
ERROR/AndroidRuntime(622):  at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485) 
ERROR/AndroidRuntime(622):  at android.os.Handler.handleCallback(Handler.java:587) 
ERROR/AndroidRuntime(622):  at android.os.Handler.dispatchMessage(Handler.java:92) 
ERROR/AndroidRuntime(622):  at android.os.Looper.loop(Looper.java:123) 
ERROR/AndroidRuntime(622):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
ERROR/AndroidRuntime(622):  at java.lang.reflect.Method.invokeNative(Native Method) 
ERROR/AndroidRuntime(622):  at java.lang.reflect.Method.invoke(Method.java:521) 
ERROR/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
ERROR/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
ERROR/AndroidRuntime(622):  at dalvik.system.NativeStart.main(Native Method) 
+0

我的數據庫有12個值我使用表格佈局列表視圖,但在第二排它再次開始從數據庫中顯示第二值,並告訴我警報部隊完成活動當我到達下來底部滾動... –

+0

不能得到你完全可以你更具體的關於你的問題... –

+0

我有三個按鈕在表格佈局和我的數據庫有12個值我想設置文本按鈕從數據庫1 - >這段代碼給出第一行的第一個三個值,第二行開始設置第一行的第二個值的按鈕上的文本,依此類推...... 2->當我使用moveToNext();與我的代碼,當我移動查看底部行它將顯示強制整理活動,當我不使用moveToNext();那麼滾動將是好的 –

回答

1

當你得到一個從數據庫返回的遊標時,你應該做一個moveToFirst();在嘗試從中獲取數據之前。試試這個

void populateFrom(Cursor c, NoteHelper helper) { 
    if (!c.moveToFirst()) return; //if no data dont do anything 
    b1.setText(helper.getNote(c)); 
     c.moveToNext(); 
    b2.setText(helper.getNote(c)); 
    c.moveToNext(); 
    b3.setText(helper.getNote(c)); 
} 
+0

謝謝,你的ans解決了我的滾動問題。但是我的數據庫有12個值,我想在按鈕上設置文本,並且你的ans中的所有行都顯示數據庫的前三個值作爲按鈕文本。 –

+0

我不完全明白你的意思..你想在12個按鈕上設置12個文本? – Rejinderi

+0

是的,我想在12個按鈕上設置12個文字 –

0

通知該異常被升高內部getNote,而不是在populateForm

您應該檢查moveToNext()的返回碼。如果您已經超過結果集的末尾(即沒有0​​),它將返回false。在這種情況下,您無法從該遊標訪問行數據(它實際上無處指向)。所以你不應該在該遊標上調用getNote,因爲它試圖從中檢索數據。