The screenshots show a
DogAdapter
,
IconAdapter
,
and
LinearLayoutAdapter
.
Nothing happens when you select a list item,
because I didn’t bother to plug an
AdapterView.OnItemClickListener
into the
ListView
.
In
Spinner
and
ListView,
our
Adapter
was an off-the-shelf
ArrayAdapter
.
An
ArrayAdapter
converts each data item to a
String
(with the Java method
toString
)
and puts the result in a
TextView
.
But the data items in this app are images,
so we will have to use a different class of
Adapter
.
In fact, we will have to write our own class of
Adapter
.
DogAdapter
.
The most important methods of the adapter are
getCount
,
which returns a number,
and
getView
,
which returns a
View
.
In
DogAdapter
,
the
View
is an
ImageView
.
Class
ImageView
is a subclass of
View
specialized for displaying an image,
just as a
TextView
is specialized for displaying text.
The
getView
method receives a
position
argument in the range 0 to 7 inclusive.
getCount
and
getView
are called by the
AdapterView
.
In this app, the
AdapterView
is a
ListView
.
See the
List
View
tutorial.
One method that is not called is
getItem
,
but I wrote it anyway just for completeness.
getItem
calls the recently deprecated one-argument
getDrawable
rather than the newfangled two-argument
getDrawable
,
because I want my app to be able to run on older Android devices.
See how Android Studio
crosses out
getDrawable
in
DogAdapter.java
?
The
minSdkVersion
is in
build.gradle
(Module: app).
MainActivity.java
plugs a
DogAdapter.java
into a
ListView
.
BaseAdapter
,
which implements interface
Adapter
:
DogAdapter.java
creates a series of
ImageView
s.
The
Drawable
in each
ImageView
comes from the project’s
resources
in the
res/drawable
folder.
IconAdapter.java
creates a series of
ImageView
s.
The
Drawable
in each
ImageView
comes from an
Activity
on the Android device.
LinearLayoutAdapter.java
creates a series of horizontal
LinearLayout
s.
Each
LinearLayout
contains an
ImageView
and a
TextView
.
R.java
activity_main.xml
:
a
RelativeLayout
contains a
ListView
.
row.xml
contains a horizontal
LinearLayout
.
It is inflated (turned into actual Java objects)
by the
inflator
in the
getView
method of the
LinearLayoutAdapter
.
styles.xml
AndroidManifest.xml
build.gradle
(Module: app).
res/drawable
folder:
sample_thumb_0.jpg
(45 × 60).
sample_thumb_1.jpg
(60 × 45)
sample_thumb_2.jpg
(60 × 40)
sample_thumb_3.jpg
(60 × 45)
sample_thumb_4.jpg
(60 × 40)
sample_thumb_5.jpg
(40 × 60)
sample_thumb_6.jpg
(60 × 45)
sample_thumb_7.jpg
(45 × 60)
sample_0.jpg
(213 × 285).
These big files are not used.
sample_1.jpg
(285 × 213)
sample_2.jpg
(285 × 191)
sample_3.jpg
(285 × 213)
sample_4.jpg
(285 × 190)
sample_5.jpg
(191 × 285)
sample_6.jpg
(285 × 213)
sample_7.jpg
(213 × 285)
Select the folder
app/res/layout
in the Android Studio
project
view.
Then pull down
File → New… → Layout resource file
File name: row.xml
Root element: LinearLayout
OK
Select the folder
app/java/edu.nyu.scps/adapter
in the Android Studio
project
view.
Then pull down
File → New… → Java Class
Create New Class
Name: DogAdapter
Kind: Class
OK
In the new file
DogAdapter.java
,
add the words
extends BaseAdapter
.
To add the four methods
getCount
,
getItem
,
getItemID
,
and
getView
,
click on the word
DogAdapter
and pull down
Code → Implement Methods…
OK
To add the constructor,
Code → Generate…
and select Constructor.
onCreate
,
change
DogAdapter adapter = new DogAdapter(this);to
IconAdapter adapter = new IconAdapter(this);
onCreate
,
change the
Adapter
to
LinearLayoutAdapter adapter = new LinearLayoutAdapter(this);In
AndroidManifest
,
add the following element to the
manifest
element.
<uses-feature android:name="android.hardware.screen.landscape">Add the attribute
android:screenOrientation="landscape"
to the
activity
element.
DogAdapter.java
,
the call to the one-argument
getDrawable
causes the following error message in the Gradle console.
Note: /Users/myname/AndroidStudioProjects/Adapter/app/src/main/java/edu/nyu/scps/adapter/DogAdapter.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Discover how to recompile with
-Xlint:deprecation
.
FYI,
-X
javac.
javac -X