My BD search placed in a listview (Arrayadapter) returns the object address and not the content

Asked

Viewed 128 times

1

I made an APP that saves the date and a series of numbers (8 numbers) in the Android database. I created an object ( with date and numbers), but when I search all objects inside the database and put in a list it returns the addresses saved.

Code of the select function:

public List<Hinos> selectHinos() {
    List<Hinos> lstHinos = new ArrayList<Hinos>();
    SQLiteDatabase db = getReadableDatabase();

    String sqlSelect = "SELECT * FROM Historico";

    Cursor c = db.rawQuery(sqlSelect, null);

    if (c.moveToFirst()) {
        do {
            Hinos hino = new Hinos();

            hino.setId(c.getInt(0));
            hino.setData(c.getString(1));
            hino.setH1(c.getString(2));
            hino.setH2(c.getString(3));
            hino.setH3(c.getString(4));
            hino.setH4(c.getString(5));
            hino.setH5(c.getString(6));
            hino.setH6(c.getString(7));
            hino.setH7(c.getString(8));
            hino.setH8(c.getString(9));

            lstHinos.add(hino);
        } while (c.moveToNext());
    }
    db.close();

    return lstHinos;
}

Receiving the return (list) I treat this way by putting in a list view:

HinosDAO dao = new HinosDAO(this);
List<Hinos> histo= dao.selectHinos();
dao.close();
adapter = new ArrayAdapter<Hinos>(Historico.this,android.R.layout.simple_list_item_1,histo);
historico.setAdapter(adapter);

But in my list view appears:

com.estudo.app.historicootd.Objetos.Hinos@b706da7,  
com.estudo.app.historicootd.Objetos.Hinos@42a3666, 
com.estudo.app.historicootd.Objetos.Hinos@671c254, 
com.estudo.app.historicootd.Objetos.Hinos@89d85fd

1 answer

2


Everything is working as you have programmed it. In your Hymns list (lstHinos), you add objects of the type Hino. Therefore, if you print this with the System.out.println, for example, you will see exactly what is being displayed in your ListView.

This is the standard of the method toString() Java that is implemented in the class Object, class of which the Hino, directly or indirectly, inherits. The ListView calls the toString() of each object to display the value in the list.

See the implementation of the method toString() class Object:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

However, you can rewrite this method within the class Hino. Printing what you want.

To rewrite, simply declare the method again (with the same signature) and place the annotation @Override. Note not required, it is useful for compiler checks and code readability.

@Override
public String toString() {
    return "Novo retorno da toString() para a classe Hino"
}
  • Ar Cantoni Thank you very much...was that detail even was just edit as wanted the output within the class that worked. Thanks msm saved the day kkk

Browser other questions tagged

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