I have a database helper class:
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
Which works fine when I want to display all the data from a button in an activity, i.e:
public void onbtnViewAllRecordsClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// Show message
showMessage("Error", "No data found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("ID " + res.getString(0) + "\n");
buffer.append("Amount " + res.getString(1) + "\n");
buffer.append("Cost " + res.getString(2) + "\n");
buffer.append("Date " + res.getString(3) + "\n\n");
}
// Show all data
showMessage("Data",buffer.toString());
}
I'm now trying to figure out how to get certain detail from the database when the app is loaded and display it on the default activity. I have an EditText on the default activity, and want to add up all the 'Amount' values in the database, and display it within the EditText.
Can someone point me in the write direction.
Aucun commentaire:
Enregistrer un commentaire