Somar dados de Banco e Mostrar em um Textview

  • Respostas:1
Álysson Alexandre
  • Posts no fórum: 2

13/01/2014, 02:48:48 via Web

Pessoal esse é o código que desenvolvi porém da erro e não mostra a soma.

método da soma na classse Gfdb2

1public Cursor somarCategoria() {
2 String SelectQuery = "select sum(valordespesa) FROM despesas";
3 SQLiteDatabase db = this.getWritableDatabase();
4 Cursor cursor = db.rawQuery(SelectQuery, null);
5 //cursor.moveToFirst();
6 return cursor;
7 }
Tentando mostrar em outra classe

1db = new GFdb2(this);
2 texto = (TextView) findViewById(R.id.tx_aqui);
3 Cursor resultado = db.somarCategoria();
4 texto.setText("Resultado: " +resultado);


Porém aparece é:

Resultado: android.database.sqlite.SQLiteCursor@43e4c240

Obrigado desde já.

Responder
Álysson Alexandre
  • Posts no fórum: 2

14/01/2014, 01:48:28 via Web

Pessoal consegui resolver o problema. Segue o código corrigido.

Na classe do Banco GFdb2.class
1public String somarCategoria() {
2 String SelectQuery = "select sum(valordespesa) FROM despesas";
3 SQLiteDatabase db = this.getWritableDatabase();
4 Cursor cursor = db.rawQuery(SelectQuery, null);
5
6
7 if (cursor.moveToNext()) {
8 total = cursor.getString(0);
9 }
10 return total;
11 }

Na Classe para mostrar:
1db = new GFdb2(this);
2 texto = (TextView) findViewById(R.id.tx_aqui);
3 texto.setText(db.somarCategoria());

Responder