jeudi 23 avril 2015

Getting the result of cursor and turning it into a string for TextView

This is my query :

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 that query in a TextView but I don't know how to, so naturally I look for an answer. I find a few answers around and come up with this :

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 the 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 got me a bit confused since the usual fix for that error is using cur.moveToFirst(); which is used in both instances... what am I doing wrong exactly?

Aucun commentaire:

Enregistrer un commentaire