问题
I am performing a sqlite database operation on click event of listview as shown below:
ListView lv1 = (ListView) findViewById(R.id.listView1);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String out = arg0.getItemAtPosition(arg2).toString();
SQLiteDatabase db = openOrCreateDatabase("mynotedb", MODE_PRIVATE, null);
String sql = "select description from notecont where title='"+out+"'";
Cursor c1 = db.rawQuery(sql, null);
int n = c1.getColumnIndex("description");
data = c1.getString(n);
if(data.equals(""))
{
data="";
}
c1.close();
send(out, data);
}
});
But on running it gives me cursor index out of bound exception. I am also mentioning the details of logcat :
07-15 22:24:05.791: E/AndroidRuntime(6932): FATAL EXCEPTION: main
07-15 22:24:05.791: E/AndroidRuntime(6932): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
07-15 22:24:05.791: E/AndroidRuntime(6932): at com.example.notepad.MainActivity$2.onItemClick(MainActivity.java:65)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.widget.ListView.performItemClick(ListView.java:3513)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.os.Handler.handleCallback(Handler.java:587)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.os.Looper.loop(Looper.java:123)
07-15 22:24:05.791: E/AndroidRuntime(6932): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 22:24:05.791: E/AndroidRuntime(6932): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 22:24:05.791: E/AndroidRuntime(6932): at java.lang.reflect.Method.invoke(Method.java:507)
07-15 22:24:05.791: E/AndroidRuntime(6932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 22:24:05.791: E/AndroidRuntime(6932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 22:24:05.791: E/AndroidRuntime(6932): at dalvik.system.NativeStart.main(Native Method)
Please suggest a fix.
回答1:
Use one of the moveTo...()
methods such as moveToFirst()
on the Cursor
before trying to access its column data. After the query, the cursor will be pointing to row index -1 which is not a valid row.
Also check the return value of the move to make sure the cursor is pointing to a valid row after the move.
For example,
Cursor c1 = db.rawQuery(sql, null);
if (c1.moveToFirst()) {
int n = c1.getColumnIndex("description");
data = c1.getString(n);
}
来源:https://stackoverflow.com/questions/25052444/cursor-index-out-of-bound-exception-on-listview-cllick-event