How to set SELECT values in TextView?

3

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 to display query results:

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 error when I run the application to run. By Debug I find that the values of class EpilogoDado is null .

There is data registered in the bank.

Thank you!

    
asked by anonymous 02.11.2016 / 00:16

1 answer

0

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 epilogueDado.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);
    
03.11.2016 / 19:28