listview单击事件上的游标索引超出绑定异常

时间:2014-07-31 06:49:08

标签: android sqlite listview indexoutofboundsexception android-cursor

我正在listview的click事件上执行sqlite数据库操作,如下所示:

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);
            }
        });

但是在运行它时会给我游标索引超出绑定的异常。我还提到了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)

请建议修复。

1 个答案:

答案 0 :(得分:3)

在尝试访问其列数据之前,在moveTo...()上使用moveToFirst()方法之一,例如Cursor。在查询之后,光标将指向行索引-1,它不是有效行。

同时检查移动的返回值,以确保移动后光标指向有效行。

例如,

Cursor c1 = db.rawQuery(sql, null);
if (c1.moveToFirst()) {
    int n = c1.getColumnIndex("description");
    data = c1.getString(n);
}