Implement the Adapter interface

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.

The class will be called 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).

Source code in Adapter.zip

  1. MainActivity.java plugs a DogAdapter.java into a ListView.
  2. Three subclasses of class BaseAdapter, which implements interface Adapter:
    1. DogAdapter.java creates a series of ImageViews. The Drawable in each ImageView comes from the project’s resources in the res/drawable folder.
    2. IconAdapter.java creates a series of ImageViews. The Drawable in each ImageView comes from an Activity on the Android device.
    3. LinearLayoutAdapter.java creates a series of horizontal LinearLayouts. Each LinearLayout contains an ImageView and a TextView.
  3. R.java
  4. activity_main.xml: a RelativeLayout contains a ListView.
  5. row.xml contains a horizontal LinearLayout. It is inflated (turned into actual Java objects) by the inflator in the getView method of the LinearLayoutAdapter.
  6. styles.xml
  7. AndroidManifest.xml
  8. build.gradle (Module: app).
  9. Bitmap resources in the res/drawable folder:
    1. sample_thumb_0.jpg (45 × 60).
    2. sample_thumb_1.jpg (60 × 45)
    3. sample_thumb_2.jpg (60 × 40)
    4. sample_thumb_3.jpg (60 × 45)
    5. sample_thumb_4.jpg (60 × 40)
    6. sample_thumb_5.jpg (40 × 60)
    7. sample_thumb_6.jpg (60 × 45)
    8. sample_thumb_7.jpg (45 × 60)
    9. sample_0.jpg (213 × 285). These big files are not used.
    10. sample_1.jpg (285 × 213)
    11. sample_2.jpg (285 × 191)
    12. sample_3.jpg (285 × 213)
    13. sample_4.jpg (285 × 190)
    14. sample_5.jpg (191 × 285)
    15. sample_6.jpg (285 × 213)
    16. sample_7.jpg (213 × 285)

Create the project

Create the layout file row.xml

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

Create class DogAdapter

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.

Things to try

  1. In onCreate, change
            DogAdapter adapter = new DogAdapter(this);
    
    to
            IconAdapter adapter = new IconAdapter(this);
    

  2. In 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.

  3. Press Run to compile and run your program. To see the Gradle console during the compilation, click on the words “Gradle Console” at the right side of the lower edge of Android Studio. When compiling 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, -Xjavac.
    javac -X