Search code examples
androidsqlitecursorint

Displaying a Cursor as a return value from a method-SQLite


I am attempting to use the following method to calculate the total of all values in a certain column in my DB.

/**
     * Method that gives the the total of all
     * Average Meditation levels in the DB
     * @return
     */
    public Cursor getTotalOfAllMedLevels(){

        SQLiteDatabase db = this.getWritableDatabase();

        String query = "SELECT SUM(avgmeditation) FROM " + TABLE_SCORE;


        Cursor c = db.rawQuery(query, null);

        return c;



    }

I am then trying to display the return value from the method within a TextView (avgMed) in a different Activity like so:

public void displayAverageOfAllMedValues() {

        Cursor c = db.getTotalOfAllMedLevels();

        avgMed.setText("" + c);

    }

But get the following output in the TextView:

android.database.sqlite.SQLiteCursor@529db063

Is there a way of converting this output to an int value, or alternatively how can I change my code to give an Int output that I want.

EDIT: Current Database creation and structure:

public class DatabaseHelper extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 10;

    // Database Name
    private final static String DATABASE_NAME = "MeditationDatabase";

    // Contacts table name
    private static final String TABLE_SCORE = "scores";

    // Contacts Table Columns names
    private static final String COL_SESSION = "sessionid";
    private static final String COL_GAMETITLE = "game";
    private static final String COL_NAME = "name";
    private static final String COL_MED = "avgmeditation";
    private static final String COL_MAX = "maxmeditation";
    private static final String COL_AVGATT = "avgattention";
    private static final String COL_MAXATT = "maxattention";
    private static final String COL_SCORE = "score";
    private static final String COL_DATE = "date";

    /**
     * Constructor
     * 
     * @param context
     */
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * Method that creates the database
     */
    @Override
    public void onCreate(SQLiteDatabase db) {



        String CREATE_TABLE_SCORE = "CREATE TABLE " + TABLE_SCORE + "(" + COL_SESSION
                + " STRING PRIMARY KEY, " + COL_GAMETITLE + " STRING, "  + COL_NAME + " STRING, " + COL_MED + " INTEGER, "
                 + COL_MAX + " INTEGER, " + COL_AVGATT + " INTEGER, " + COL_MAXATT + " INTEGER, "  + COL_SCORE +  " INTEGER, " + COL_DATE + " STRING " + ")";
        db.execSQL(CREATE_TABLE_SCORE);

    }

Solution

  • A cursor is like an array, there are all the objects that your select found in it. You must do something like this:

    public void displayAverageOfAllMedValues() {
    
        Cursor c = db.getTotalOfAllMedLevels();
    
        if( c != null && c.moveToFirst )
        {
    
             avgMed.setText("" + c.getInt( 0 ) );
    
        }
    
    }