Display data in textView

1

I have a database in an application of android , I'd like the field name in the table to fill in the text of the TextView . I saw some examples but I could not implement it, getReadableDatabase().rawQuery(sqlNome, null); always turns red, ie I can not use it. It's just a given that will be in the table.

Here is the code I tried to implement:

criaBanco = new CriaBanco(getBaseContext());
sqlNome = "select nome from tabela";
Cursor cursor = getReadableDatabase().rawQuery(sqlNome, null);

textView = (TextView) findViewById(R.id.txt1);
if (!criaBanco.NOME.isEmpty()) {
    textView.setText(sqlNome);
}
else {
    textView.setText("NOME");
}

cursor.close();
    
asked by anonymous 16.06.2016 / 22:37

2 answers

1

getReadableDatabase() is a method of class SQLiteOpenHelper .

I assume that the BankBank class inherits from it, so you can access this method and run query , like this:

Cursor cursor = criaBanco.getReadableDatabase().rawQuery(sqlNome, null);
    
17.06.2016 / 15:05
1

Good morning Henrique, The getReadableDatabase() method must run in your database.

SQLiteDatabase db = criaBanco.getReadableDatabase();
Cursor cursor = db.rawQuery(sqlNome,null);
 if (cursor != null && cursor.moveToFirst()) {
                       cursor.moveToFirst();
                       String txt = cursor.getString(cursor.getColumnIndex(nomeColuna));

                   }

This should return you to String and then just use textView.setText(txt);

The above code returns only the first result, your select seems to allow all data to return, if you want to return multiple records, after cursor.moveToFirst() do one of while with condition moveToNext()

do{

}while(cursor .moveToNext());
    
17.06.2016 / 15:02