The search for my DB placed in a listView (Arrayadapter) returns the address of the object and not the content

1

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

Function code select:

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;
}

Getting the return (list) I deal like this by putting it 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 it 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
    
asked by anonymous 02.02.2018 / 13:15

1 answer

2

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

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

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

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

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

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

@Override
public String toString() {
    return "Novo retorno da toString() para a classe Hino"
}
    
02.02.2018 / 16:16