Reputation:
I'd like to get the total of all records. I'd be getting the records from my meal_serving. I'd like to add up all the values. For example, please check this:
Meal Serving
1st row - 8
2nd row - 2
3rd row - 3
Total is: 13
This is what I've got:
public String getTotal() {
String[] column =
new String[]{ KEY_SERVING, "sum(meal_serving)" };
Cursor c =
ourDatabase.query( DATABASE_TABLE, column, null, null, null, null, null );
String result = "";
int iSERVING = c.getColumnIndex( KEY_SERVING );
for ( c.moveToFirst(); ! c.isAfterLast(); c.moveToNext() ){
result = result + c.getString( iSERVING );
}
return result;
}
But only the last record is showing. What's wrong?
Upvotes: 0
Views: 1819
Reputation: 8518
You'll need to do
String[]{ "sum(meal_serving) as " + KEY_SERVING };
Instead of your current column string array. That will generate the sum and name it so you can retrieve it.
You might need to make up a unique name if KEY_SERVING is one of your other columns too.
Upvotes: 2