[android] using selection widgets
DESCRIPTION
1. Introducing Adapters 1.1 Adapters 1.2 Using ArrayAdapter 1.3 Using ArrayAdapter (cont.) 1.4 Example 3.1 1.5 Custom ArrayAdapter 2. Using Selection Widgets 2.1 ListView 2.2 ListView (cont.) 2.3 Example 3.2 2.4 GridView 2.5 GridView (cont.) 2.6 Example 3.3 2.7 Spinners 2.8 Spinners (cont.) 3. Exercise 3TRANSCRIPT
![Page 1: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/1.jpg)
Android Programming
Using Selection Widgets
Lesson 3
NGUYEN The Linh
![Page 2: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/2.jpg)
Android Programming
Contents
Introducing Adapters 1
Using Selection Widgets 2
Exercise 3 3
2
![Page 3: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/3.jpg)
Android Programming
Using Selection Widgets
Introducing Adapters
3
![Page 4: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/4.jpg)
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
![Page 5: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/5.jpg)
Android Programming
Introducing Adapters
Using ArrayAdapter (cont.)
5
![Page 6: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/6.jpg)
Android Programming
Introducing Adapters
Example 3.1
Using Basic ArrayAdapter
Example 3.1 project
• MainActivity.java
• activity_main.xml
6
![Page 7: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/7.jpg)
Android Programming
Introducing Adapters
Custom ArrayAdapter
You can subclass ArrayAdapter and override getView() to “roll
your own” views:
7
![Page 8: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/8.jpg)
Android Programming
Using Selection Widgets
Using Selection Widgets
8
![Page 9: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/9.jpg)
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
![Page 10: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/10.jpg)
Android Programming
Using Selection Widgets
10
![Page 11: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/11.jpg)
Android Programming
Using Selection Widgets
ListView (cont.)
Custom ArrayAdapter for binding more data
See Example 3.2
11
![Page 12: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/12.jpg)
Android Programming
Using Selection Widgets
Example 3.2
Custom ArrayAdapter
Example 3.2 project
• MainActivity.java
• Person.java
• MyArrayAdapter.java
• activity_main.xml
• list_item.xml
12
![Page 13: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/13.jpg)
Android Programming
Using Selection Widgets
13
![Page 14: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/14.jpg)
Android Programming
Using Selection Widgets
14
Better. Stronger. Faster.
Using convertView
![Page 15: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/15.jpg)
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
![Page 16: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/16.jpg)
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
![Page 17: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/17.jpg)
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
![Page 18: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/18.jpg)
Android Programming
Using Selection Widgets
Example 3.3
GridView
Example 3.3 project
• MainActivity.java
• MyArrayAdapter.java
• activity_main.xml
• gridview_item.xml
18
![Page 19: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/19.jpg)
Android Programming
Using Selection Widgets
Spinners
Spinners provide a quick way to select one value from a set.
19
![Page 20: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/20.jpg)
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
![Page 21: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/21.jpg)
Android Programming
Exercise 3
21
![Page 22: [Android] Using Selection Widgets](https://reader034.vdocuments.net/reader034/viewer/2022042515/555ac6c6d8b42ab1128b50d1/html5/thumbnails/22.jpg)
Android Programming