0

我是 Android 新手,并试图从这样的数据库中选择一行:

public Cursor getEntry(int id){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor entry = db.query(TABLE_NAME, new String[]{
            "id", "title", "description", "image_path"
    }, "id" + "=?", new String[]{ String.valueOf(id) }, null, null, null, null);

    return entry;
}

并尝试像这样输出它:

    DatabaseHelper db = new DatabaseHelper(this);
    TextView tv = new TextView(this);
    if (db.getEntry(1) != null){
        while (db.getEntry(1).moveToFirst()){
            tv.setText("" + db.getEntry(1).getString(1));
        }
    }
    setContentView(tv);

和错误:

ERROR/AndroidRuntime(1850): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.untitled1/com.example.untitled1.DisplayMessageActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    at com.example.untitled1.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:28)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    ... 11 more

问题是什么?

谢谢

4

3 回答 3

2
while (db.getEntry(1).moveToFirst()){
    tv.setText("" + db.getEntry(1).getString(1));

您正在执行两次查询,每次执行db.getEntry(1). 所以第二次,你调用getString(1)了一个没有正确设置的不同的光标moveToFirst()

只需调用一次,然后将生成的 Cursor 存储在局部变量中。另外,完成后不要忘记安全地关闭光标。

Cursor c = db.getEntry(1);
try {
    if (c.moveToFirst()) 
        tv.setText(c.getString(1));
} finally {
    c.close();
}
于 2013-04-25T16:14:10.833 回答
1

你只需要查询一次,为什么要打getEntry()几次电话?并且while(cursor.moveToFirst())没有意义,如果Cursor有结果,这是一个无限循环。

Cursor cursor = getEntry(1);
try {
    if (cursor.moveToNext()){
       tv.setText(cursor.getString(1));
    }
} finally {
    cursor.close(); // Close the Cursor when you're finished with it, or use a Loader.
}
于 2013-04-25T16:16:42.857 回答
1

尝试这个

moveToFirst()db.getEntry(1) 调用两次将返回另一个游标,因此当您调用第一个游标并访问第二个游标时,它不会移动到 firstRow

Cursor cursor=db.getEntry(1);

if(cursor.moveToFirst())
{
 tv.setText("" + cursor.getString(1));
}

注意:查询方法永远不会将 Cursor 返回为 Null。所以无需检查 Null。

于 2013-04-25T16:15:34.837 回答