Using ListView, GridView, and Adapters
The ListView
and GridView
are both descendants of ViewGroup
, but they are used more like a View since they are data driven. In other words, rather than defining all the possible Views that might fill a ListView
(or GridView
) at design time, the contents are created dynamically from the data passed to the View. (The layout of the ListItem
might be created at design time to control the look of the data during runtime.)
As an example, if you needed to present a list of countries to a user, you could create a LinearLayout
and add a button for each country. There are several problems with this approach: determining the countries available, keeping the list of buttons up to date, having enough screen space to fit all the countries, and so on. Otherwise, you could create a list of countries to populate a ListView
, which will then create a button for each entry.
We will create an example, using the second approach, to populate a ListView
from an array of country...