0

I have this class

private SQLiteDatabase dbCategoria;
DAOcategoria dao;

@Override
protected void onCreate(Bundle savedInstanceState) {
    //stuff
    SQLiteDatabase dbCategoria;
    dbCategoria = openOrCreateDatabase("Lista_gastos.db", Context.MODE_PRIVATE, null);
    dao = new DAOcategoria(dbCategoria);
    //more stuff
    buscadados(select nome from categoria)
}

public ArrayList<String> buscaDados(String sql){
    Cursor dados = dao.RetornarDados(sql);
    ArrayList<String> res = new ArrayList<String>();
    dados.moveToFirst();
    do{
        // HERE is my problem i guess
        res.add(dados.getString(dados.getColumnIndex("nome")));
    }while (dados.moveToNext());
    dados.close();
    return res;
}

public Cursor RetornarDados(String sql) {
    Cursor dados = null;
    try
    {
        dados = db.rawQuery(sql, null);
        return dados;
    }
    catch(Exception ex)
    {
        return dados;
    }
}

I'm putting this String list on a spinner just to see the results, but what those string are returning are "android.support.v7.widget.App...' , however i wanna the values inside the table, i really can't see what i'm doing wrong

here's how i'm inserting new info

public void inserir(){
    EditText txtCat = (EditText)findViewById(R.id.txtCat);
    StringBuilder sql = new StringBuilder();
    if (!(txtCat.equals(""))) {
        sql.append("INSERT INTO categoria(nome) VALUES (");
        sql.append("'" + txtCat + "')");

        if (dao.ExecutarComando(sql.toString()) == true) {
            Toast.makeText(getBaseContext(), "Nova categoria criada com sucesso", Toast.LENGTH_SHORT).show();
            txtCat.setText("");
        } else {
            Toast.makeText(getBaseContext(), sql.toString(), Toast.LENGTH_LONG).show();
        }
    }
Raul Quinzani
  • 493
  • 1
  • 4
  • 16
  • 1
    How are you storing the values in the database? That looks to be the more likely culprit. It looks like you might be calling `toString()` directly on an `EditText`, or similar. – Mike M. Jun 19 '17 at 03:33
  • 1
    `txtCat` is an `EditText`. Concatenating that like you are with the `String` literals is implicitly calling `toString()` directly on the `EditText`, which results in what you're seeing. Use `txtCat.getText().toString()` there instead. – Mike M. Jun 19 '17 at 04:11
  • 1
    You will also need to change `txtCat.equals("")` in the `if`, as that will never be true. – Mike M. Jun 19 '17 at 04:20
  • 1
    I can't believe I foegot the gettext.toatring method, thank you man, I was getting out of ideas, and forgot to look at the basics – Raul Quinzani Jun 19 '17 at 04:25

0 Answers0