I need help with summing all the values in one of the columns(amount) in my database. i can a get a particular value. .but i need sum of a specific column, somebody tell me what I'm doing wrong
This is my code
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
databaseAdapter.open();
ArrayList<String> records = databaseAdapter.fetchAllRecords();
if (records.size() > 0) {
et.setText(records.get(0));
}
databaseAdapter.close();
}
});
//Create our database by opening it and closing it
DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
databaseAdapter.open();
databaseAdapter.close();
}
private Object append(CharSequence text) {
// TODO Auto-generated method stub
return null;
}
/** Create a new dialog for date picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
}
return null;
}
}
This is the database part DatabaseAdapter.java
package com.example.androidtablayout;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;
public class DatabaseAdapter {
private Context context;
private SQLiteDatabase database;
private DatabaseOpenHelper dbHelper;
public DatabaseAdapter(Context context) {
this.context = context;
}
public DatabaseAdapter open() throws SQLException {
dbHelper = new DatabaseOpenHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public long createRecord(String text,int j,String text1,String text2) {
ContentValues contentValue = new ContentValues();
contentValue.put("date", text);
contentValue.put("amount", j);
contentValue.put("des", text1);
contentValue.put("category", text2);
return database.insert("Extable", null, contentValue);
}
public boolean updateRecord(long rowId,String text,int j,String text1,String text2) {
ContentValues contentValue = new ContentValues();
contentValue.put("date", text);
contentValue.put("amount", j);
contentValue.put("des", text1);
contentValue.put("category", text2);
return database.update("Extable", contentValue, "_id =" + rowId, null) > 0;
}
public boolean deleteRecord(long rowId) {
return database.delete("Extable", "_id =" + rowId, null) > 0;
}
public ArrayList<String> fetchAllRecords() {
Cursor cursor = database.query("Extable", new String[] { "_id", "date", "amount", "des", "category"},
null, null, null, null, null);
ArrayList<String> records = new ArrayList<String>();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
records.add(cursor.getString(1));
cursor.moveToNext();
}
cursor.close();
return records;
}
public String fetchRecord(long rowId) throws SQLException {
Cursor mCursor = database.query(true, "Extable", new String[] { "_id",
"date","amount", "des","category" }, "_id ="+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
return (mCursor.getString(1));
}
return null;
}
public Cursor rawQuery(String string, Object object) {
// TODO Auto-generated method stub
return null;
}
Pls anyone help to me.
Thank u
See Question&Answers more detail:
os