java – How to set SELECT values ​​not TextView?

Question:

I have the following class for database queries:

package com.example.tais.books.Dados;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/**
 * Created by Tais on 27/10/2016.
 */

public class EpilogoDado {

    private BDHelper sqlite;
    private SQLiteDatabase db;

    public int total;
    public int nt;
    public int lv;

    public EpilogoDado(Context context){
        sqlite = new BDHelper(context);
        db = sqlite.getWritableDatabase();
    }

    public int somarPagina(){

        total = 0;

        Cursor pag = db.rawQuery("SELECT SUM pagina FROM livro",null);

        if (pag.moveToFirst()) {
            total =  pag.getInt(0);
            pag.close();
        }

        return total;
    }

    public int contarNota(){

        Cursor nota = db.rawQuery("SELECT COUNT * FROM nota", null);

        if(nota.moveToFirst()){
            nt = nota.getCount();
            nota.close();
        }
        return nt;
    }

    public int contarLivro(){

        Cursor livro = db.rawQuery("SELECT COUNT * FROM livro", null);

        if (livro.moveToFirst()){
            lv = livro.getCount();
            livro.close();
        }
        return lv;
    }
}

And this to display the results of the queries:

package com.example.tais.books;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.TextView;

import com.example.tais.books.Dados.EpilogoDado;

public class epilogoBook extends AppCompatActivity {

    EpilogoDado bd;

    TextView paginometro;
    TextView notast;
    TextView livrot;
    TextView ntplv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_epilogo_book);

        bd = new EpilogoDado(getApplicationContext());

        paginometro = (TextView)findViewById(R.id.TextViewPg);
        livrot = (TextView)findViewById(R.id.TextViewLv);
        notast = (TextView)findViewById(R.id.TextViewNt);
        ntplv = (TextView)findViewById(R.id.TextViewNpL);

        paginometro.setText(Integer.parseInt(String.valueOf(bd.somarPagina())));
        livrot.setText(Integer.parseInt(String.valueOf(bd.contarLivro())));
        notast.setText(Integer.parseInt(String.valueOf(bd.contarNota())));
        ntplv.setText(Integer.parseInt(String.valueOf(bd.contarNota()/bd.contarLivro())));
    }

}

And it's giving an error when I run, the application stops running. By Debug I find that the values ​​of the EpilogoDado class is null .

There is data registered in the bank.

Grateful!

Answer:

In epilogoBook.java

paginometro.setText(String.valueOf(bd.somarPagina()));
livrot.setText(String.valueOf(bd.contarLivro()));
notast.setText(String.valueOf(bd.contarNota()));
ntplv.setText(String.valueOf(bd.contarNota()/bd.contarLivro()));

In epilogoData.java

Cursor pag = db.rawQuery("SELECT SUM(pagina) FROM livro",null);
Cursor nota = db.rawQuery("SELECT COUNT(*) FROM nota", null);
Cursor livro = db.rawQuery("SELECT COUNT(*) FROM livro", null);
Scroll to Top