The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


AdapterViews and Adapters

If you want the user to choose something out of a collection of somethings, you could use a bunch of RadioButton widgets. However, Android has a series of more flexible widgets than that, particularly for scenarios where the collection is not knowable when you are writing your app: the results of a Web service call, the results of a database query, etc.

Popular visual representations of collections include:

Android has two major solutions for this sort of problem. RecyclerView is the newer solution. For lists, grids, and trees, you should start by considering RecyclerView. However, RecyclerView comes in the form of a library, and you often use other libraries to extend it (e.g., for supporting trees). Since we have not covered libraries yet, we will hold off discussing RecyclerView until a bit later in the book.

The classic solution involved subclasses of AdapterView, such as:

and many more.

At their core, these are ordinary widgets. You will find them in your tool palette of your IDE’s graphical layout editor, and can drag them and position them as you see fit.

Their base AdapterView class is so named because it partners with objects implementing the Adapter interface to determine what choices are available for the user to choose from.

RecyclerView also uses adapters, though with a slightly different API than what AdapterView uses. And there are some scenarios — such as drop-down list – where RecyclerView is not really an option, and where AdapterView (particularly Spinner) will be the best choice. So, in this chapter, we will examine the AdapterView family, partly for historical reasons, partly for background for learning about RecyclerView, and partly for ongoing use in specific scenarios.

Adapting to the Circumstances

An Adapter is your bridge between your model data and that data’s visual representation in the AdapterView:

Android ships with several Adapter classes ready for your use, where the different adapter classes are designed to “adapt” different sorts of collections (e.g., arrays versus results of database queries). Android also has a BaseAdapter class that can serve as the foundation for your own Adapter implementation, if you need to “adapt” a collection of data that does not fit any of the Adapter classes supplied by Android.

Using ArrayAdapter

The easiest adapter to use is ArrayAdapter — all you need to do is wrap one of these around a Java array or java.util.List instance, and you have a fully-functioning adapter:

String[] items={"this", "is", "a", "really", "silly", "list"};
new ArrayAdapter<String>(this,
                         android.R.layout.simple_list_item_1,
                         items);

One flavor of the ArrayAdapter constructor takes three parameters:

  1. The Context to use (typically this will be your activity instance)
  2. The resource ID of a view to use (such as a built-in system resource ID, as shown above)
  3. The actual array or list of items to show

By default, the ArrayAdapter will invoke toString() on the objects in the list and wrap each of those strings in the view designated by the supplied resource. android.R.layout.simple_list_item_1 simply turns those strings into TextView objects. Those TextView widgets, in turn, will be shown in the list or spinner or whatever widget uses this ArrayAdapter. If you want to see what android.R.layout.simple_list_item_1 looks like, you can find a copy of it in your SDK installation — just search for simple_list_item_1.xml.

We will see in a later section how to subclass an Adapter and override row creation, to give you greater control over how rows and cells appear.

Lists of Naughty and Nice

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Clicks versus Selections

The preview of this section is in an invisible, microscopic font.

Spin Control

The preview of this section will not appear here for a while, due to a time machine mishap.

Grid Your Lions (Or Something Like That…)

The preview of this section left for Hollywood to appear in a reality TV show.

Fields: Now With 35% Less Typing!

The preview of this section was whisked away by a shark-infested tornado.

Customizing the Adapter

The preview of this section was lost due to a rupture in the space-time continuum.

Visit the Trails!

The preview of this section took that left turn at Albuquerque.