How to set SELECT values in Textview?

Asked

Viewed 298 times

3

I have the following class for bank appointments:

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

There is data registered in the database.

Grateful!

  • Hello @Tais Caires, welcome to Stack Overflow! You could attach the logcat with the error along with your question?

  • saveInstadedState = null - bd = null - paginometro = null - too Textview = null @regmoraes

  • Epilogodado.java:30 epilogoBook.java:32

  • actually it would be better to copy and paste the logcat directly into the question by editing it.

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 epilogyDado.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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.