0

From the example of the book I am studying from

public static final Drink[] drinks = {
   new Drink ("Latte", "A couple of espresso shots with steamed milk ",
                      R.drawable.latte),
   new Drink ("Cappuccino", "Espresso, hot milk, and a steamed milk foam",
                      R.drawable.cappuccino),
   new Drink ("Filter", "Highest quality beans roasted and brewed fresh",
                      R.drawable.filter)
}; 

First we have this method returning an array in the Drink class and in an activity we are supposed to retrieve data from it by using an adapterarray and here's that code:

  ArrayAdapter<Drink> listAdapter= new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, Drink.drinks);
    ListView listDrinks = (ListView) findViewById(R.id.list_drinks);
    listDrinks.setAdapter(listAdapter);

What I don't understand is why does this ArrayAdapter only retrieve the name of the drinks rather than the whole details in a list view? I can't see anywhere in the code where we specify only to retrieve the names.

2 Answers2

0

If you click into android.R.layout.simple_list_item_1 layout, you can see that: In that layout, it just contain only 1 text field, so it just can display only one property of your object.

If you want to display more information or custom your view, try this:

https://stackoverflow.com/a/6306901/11888658

Brian H.
  • 1,603
  • 11
  • 16
0

If you go back and look at the rest of the chapter from Android Developmet 2015 that you've copied the code from. You should see that the Drink class has a ToString method that return's this.name and that is the "name of the drink" by default that is is used as the representation by your list adapter.

LFMekz
  • 593
  • 8
  • 10