其他分享
首页 > 其他分享> > android – 获取光标的结果并将其转换为TextView的字符串

android – 获取光标的结果并将其转换为TextView的字符串

作者:互联网

这是我的查询:

Cursor nextdate(String Date) {
    SQLiteDatabase db = this.getReadableDatabase();
    String[] params = new String[]{String.valueOf(Date)};
    Cursor cur = db.rawQuery(" SELECT MIN (" + colDateDue + ") FROM " + PAYMENTS + " WHERE " + colDateDue + ">=?", params);
    cur.moveToFirst();
    return cur;
}

我想在TextView中显示该查询的结果,但我不知道如何,所以我自然而然地寻找答案.我找到了几个答案并提出了这个问题:

DatabaseHelper db = new DatabaseHelper(this);
String str = "";
    if (c!= null) {
        if (c.moveToFirst()) {
            str = c.getString(c.getColumnIndex(db.colDateDue);
        }
    }
TextView.setText(str);

但是我得到了错误

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

这让我有点困惑,因为通常修复该错误是使用cur.moveToFirst();这两种情况都使用了……我到底做错了什么?

解决方法:

试试这样:

将列索引保持为0,因为该游标只有一列.

DatabaseHelper db = new DatabaseHelper(this);
String str = "";
    if (c!= null) {
        if (c.moveToFirst()) {
            str = c.getString(0);
        }
    }
TextView.setText(str);

标签:android,sqlite,android-cursor
来源: https://codeday.me/bug/20190609/1205104.html