光标索引出界异常的列表视图cllick事件光标、视图、索引、异常

2023-09-04 23:39:21 作者:浪者

我表演的ListView的单击事件SQLite数据库的操作,如下所示:

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

但在运行它给我的光标索引出界异常。我也提到的logcat的细节:

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)

请提出一个修正。

推荐答案

使用的一种 MOVETO ..​​.()的方法,如 moveToFirst ()光标试图访问其列数据之前。查询后,将光标指向行索引-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.

例如,

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