No query appears on Android

0

I have an intoento problem to make a query and print it in an Edit Text. But I get a message in the Edit Text instead of my query. This is my method to make the query.

public String[] ob_lab(){
    objBD = new Bd(nContext, "Laboratorios", null, 1);
    db = objBD.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT Descripcion FROM Laboratorios WHERE ID = (SELECT MAX(ID) FROM Laboratorios);", null);
    cursor.moveToFirst();
    ArrayList<String> names = new ArrayList<>();
    while(!cursor.isAfterLast()) {
        names.add(cursor.getString(cursor.getColumnIndex("Descripcion")));
        cursor.moveToNext();
    }
    cursor.close();
    return names.toArray(new String[names.size()]);
}

in this other I call it to print it

public class consulta extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consulta);


    TextView Lab=(TextView)findViewById(R.id.lab_con);
    ConexionBD O1=new ConexionBD(this);
    O1.ob_lab();
    Lab.setText(O1.toString());

and this appears in the TextView

! ( link )

    
asked by oscar ramirez 13.11.2016 в 21:48
source

1 answer

1

Your method onLab() returns an array type string therefore you must assign in a variable of the same type the result of your call

String[] resultado = O1.ob_lab();

Then you look in that variable resultado dl value that you want for example resultado[0] but your array has many values therefore you must verify what value you want.

And finally

Lab.setText(resultado[0]);

EDITING

When I see your query I realize that you are looking for the value description of your last lab stored in the database, therefore eliminate your while of your method and change the return of your function to a single value, string

public String ob_lab(){
    objBD = new Bd(nContext, "Laboratorios", null, 1);
    db = objBD.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT Descripcion FROM Laboratorios WHERE ID = (SELECT MAX(ID) FROM Laboratorios);", null);
    cursor.moveToFirst();
    string descripcion = cursor.getString(cursor.getColumnIndex("Descripcion"));
    cursor.close();
    return descripcion;
}

And finally

Lab.setText(ob_lab());
    
answered by 13.11.2016 / 22:12
source