4. Android Programming
Introducing Adapters
Adapters are bridging classes that bind data to user-
interface Views.
Using ArrayAdapter
The ArrayAdapter is a generic class that binds AdapterViews to an array of
objects.
By default, the ArrayAdapter binds the toString() value of each object to a
TextView control defined within a layout.
To apply an Adapter to an AdapterView-derived class, you call the View’s
setAdapter() method, passing in an Adapter instance, as shown in the snippet
below:
4
9. Android Programming
Using Selection Widgets
ListView
The classic Android ListView is a plain list of text.
However, you can have a list whose rows are made up of icons,
or icons and text, or check-boxes and text, or whatever you
want.
Update Example 3.1 project
9
15. Android Programming
Using Selection Widgets
GridView
As the name suggests, GridView gives you a two-dimensional
grid of items to choose from.
You have moderate control over the number and size of the
columns; the number of rows is dynamically determined based
on the number of items the supplied adapter says are available
for viewing.
15
16. Android Programming
Using Selection Widgets
GridView (cont.)
Properties
• android:numColumns spells out how many columns there are, or, if you
supply a value of auto_fit, Android will compute the number of columns
based on available space and the following properties.
• android:verticalSpacing and its counterpart android:horizontalSpacing
indicate how much whitespace there should be between items in the grid.
• android:columnWidth indicates how many pixels wide each column should
be.
16
17. Android Programming
Using Selection Widgets
GridView (cont.)
Properties (cont.)
• android:stretchMode; this should be columnWidth to have the columns
take up available space or spacingWidth to have the whitespace between
columns absorb extra space.
Otherwise, the GridView works much like any other selection
widget—use setAdapter() to provide the data and child views.
17
18. Android Programming
Using Selection Widgets
Example 3.3
GridView
Example 3.3 project
• MainActivity.java
• MyArrayAdapter.java
• activity_main.xml
• gridview_item.xml
18
20. Android Programming
Using Selection Widgets
Spinners (cont.)
The choices you provide for the spinner can come from any
source, but must be provided through an SpinnerAdapter, such
as an ArrayAdapter
20