mardi 25 août 2015

How to iterate through rows in a cursor object android?

Hi I'm new to asking questions so not sure if this is phrased right, so i aplogyise in advance . Im trying to iterate through the rows of my cursor object. from the following query:

public Cursor getSessionTimes (String search) {

    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = {dbHelper.COLUMN_SESSION_TIME};
    Cursor res= db.query(dbHelper.TABLE_JOURNEY, columns, dbHelper.COLUMN_HEALTH_CARE_NUMBER + " = '" + search + "'", null,
        null, null, null);

    return res;
}

This query returns 1 column and 3 rows with the getSessionTimes.getcount () method. I want to return these three rows in sequence to get the total and average of the values held in these rows. The following code is what i have been trying but it just returns the first value for all three values.

Cursor cursor3 = databasehelper.getSessionTimes(HcNO);

loop = cursor3.getCount();


String test[] = new String[loop];


cursor3.moveToFirst();
while (!cursor3.isAfterLast()) {
    test[index] = cursor3.getString(0);
    index++;
    cursor3.moveToNext();

}



System.out.println("Value1 " + test[0]);
System.out.println("Value2 " + test[1]);
System.out.println("Value3 " + test[3]);

If I move the cursor3.getString(0); to cursor3.getString(1); I get a cursor error. Im really stuck on this and any help would greatly be appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire