Lesson Weekend

As we discussed when learning about Android XML Layouts, a ListView is a layout that contains a list of scrollable items. However, in order to get those items into that scrollable list, we need to use something called an adapter. In Android, an adapter is essentially a bridge between the components of your user interface, and the data that your user interface should display.

Note: Don't worry about implementing the code seen in this lesson into MyRestaurants yet; we'll walk through that process together in the next lesson.

ArrayAdapter

One of the most common adapters in Android is the ArrayAdapter. The ArrayAdapter is responsible for taking a Java ArrayList of objects from our business logic and converting them into View objects that may be displayed in our user interface. Each item in the ArrayList is converted to an individual list item View object in our ListView layout.

Implementing an Array Adapter

To use the ArrayAdapter, we need to initialize it and attach it to the ListView whose list items it will be responsible for. To create a new ArrayAdapter we must provide its constructor at least three things: The current context (represented by this), a layout for the list it will create, and the ArrayList of items it will be responsible for adapting into the ListView. This looks something like this:

...
public class RestaurantsActivity extends AppCompatActivity {
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mListView = (ListView) findViewById(R.id.listView);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, restaurants);
        mListView.setAdapter(adapter);
...

Here, we declare our mListView member variable, define it in onCreate() by locating the specific ListView by its ID with the findViewById() method. Then, we create a new ArrayAdapterproviding three arguments: this, which represents the current context, android.R.layout.simple_list_item_1, which is a layout built into Android that provides standard appearance for text in a list, and an ArrayList called restaurants (not seen here). Then, the line mListView.setAdapter(adapter); instructs the application that the adapter we've just created is responsible for taking the individual items in the restaurants ArrayList, and displaying them in the current context's layout (using the simple_list_item_1 layout).

Note: Don't worry about implementing this into our ongoing MyRestaurants project yet; we'll walk through that process together in the next lesson!

Recycling

Beyond allowing us to create a clean and aesthetically-pleasing user interface, using adapters and ListViews in this fashion offers additional functionality as well! As you're probably already aware, developing Android applications differs from developing web applications in many ways. One of these ways is working around the limited memory and resources mobile devices have compared to desktop or laptop computers.

When we display a list of many items using a ListView, it populates with individual View objects for each list item containing data provided by its corresponding adapter. The ListView only instantiates enough individual list items to fill its entire height. After that, no additional list items are created in memory. Instead, as the user scrolls down through the list, items that leave the screen are kept in memory. (These are called ScrapViews.) Then, every new row that appears in the screen as the user scrolls actually re-uses a ScrapView, but with updated data from the adapter. This process is aptly known as recycling.

Therefore, if we had a list of thousands of items, but only 5 items were visible at the time as the user scrolls, only 5 Views are ever held in memory. Pretty cool, huh?

In the next lesson we'll walk through creating a hard-coded ArrayList of restaurants in our application, and instantiating a ArrayAdapter to display each restaurant in a ListView. In future lessons we'll replace this hard-coded list with data from the Yelp API.

Additional Resources

Terminology


  • Adapter: Essentially a bridge between the components of your user interface, and the data that your user interface should display.

  • ArrayAdapter`: Responsible for taking an ArrayList of objects from our business logic and converting them into View objects to be displayed in our User Interface.

  • Recycling: The act of an adapter re-using the individual Views of a ListView as the user scrolls through a list. After the ListView has created enough individual list items to fit the full height of the screen, it simply re-uses existing list items to display the new list items instead of creating more.

  • ScrapViews: During the process of recycling, a View that leaves the screen, but is kept in memory to be re-used.

Example


...
public class RestaurantsActivity extends AppCompatActivity {
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mListView = (ListView) findViewById(R.id.listView);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, restaurants);
        mListView.setAdapter(adapter);
...

The code above...

  • Declares an mListView member variable
  • Defines it in onCreate() by locating the specific ListView by its ID with the findViewById() method.
  • Creates a new ArrayAdapter and provides three arguments: this (the current context), android.R.layout.simple_list_item_1, (a layout built into Android that provides standard appearance for text in a list), and an ArrayList called restaurants.

Additional Resources