Getting cursor result and turning it into a string for TextView

This is my request:

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

I want to display the result of this query in a TextView, but I don’t know how to do this, so naturally I am looking for an answer. I will find some answers and come up with the following:

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

But I get an error

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.

Which confused me a bit, since the usual fix for this error is using cur.moveToFirst();, which is used in both cases ... what am I doing wrong?

+4
source share
2 answers

try like this:

save the column index as 0 because this cursor will have only one column.

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

db.colDateDue. . :

str = c.getString(0);
+2

Source: https://habr.com/ru/post/1584250/


All Articles