In the past on Styling Android we have covered a variety of topics which make use of Adapters. In the series’ on ListView, ViewPager and ActionBar (to name but a few) we’ve used Adapters but kept the Adapter implementation really simple so that we may focus on the subject in question. However, on a few of these articles there are comments asking how to do different behaviours which actually require changes to the Adapter being used rather than the feature being covered in that particular article. In this series we’ll have a detailed look at Adapters, and the power and versatility that they provide us.
To understand what Adapters do, let’s think about a simple ListView. The ListView itself is a container for a number of items, and the data for these items may come from vastly different sources. For example one ListView may contain a list of static items which never change throughout the entire lifespan of the app, whereas another ListView may contain a list of items from a SQLite database. In order to keep ListView agnostic about the source of the data for its items an abstraction layer exists, and this is the Adapter. So the role of the Adapter is to keep the control (in this case the ListView) decoupled from the data that it requires to create its child elements.
Part of the flexibility of the Adapter architecture is that polymorphism is used to provide abstraction from the data source. Up until now we’ve only referred to Adapters in that abstract form, but in practise we’ll need to use a concrete Adapter implementation depending on the nature of the data and where it is sourced from. As far as our ListView is concerned the Adapter is simply a ListAdapter, but we can actually provide any concrete Adapter implemenation that we like provided it implements the ListAdapter interface.
An example would help here. We’ll put together a simple app which will allow us to demonstrate various Adapter implementations and uses, and we’ll use List Navigation in the ActionBar to enable us to switch between different fragments. We’re going to focus purely on the Adapter here, but if you require further information on List Navigation then take a look here.
Arguably the simplest for of Adapter that we can use is one which maps to simple, static values:
protected void onCreate(Bundle savedInstanceState)
final String names = getResources()
final String classes = getResources()
ArrayAdapter<String> adapter =
public boolean onNavigationItemSelected(
int itemPosition, long itemId)
FragmentTransaction tx =
All we are doing here is loading a String array from resources, and we build an ArrayAdapter from that.
ArrayAdapter is a generic class which provides an Adapter based upon a simple array of objects. In our case we’re wrapping a String array which we obtain from resources. The constructor that we’re using takes 3 parameters: The context in which the Adapter will be used, a layout to use (in this case we’re using a standard layout from the OS), and the array of objects to use.
What goes on under the bonnet is that ArrayAdapter will create a new layout (actually this is not strictly true, but we’ll return to this in a later article) for each item in the list objects, each layout will be inflated from the resource ID that we provided. It will then look for a TextView with the id “android:id/text1” (which exists in the layout – see here for proof) and set the text value of that text view to the value of the item in the list. In our case the items are all String values, but for other object classes ArrayAdapter will use the value returned by the
To get this to work add a couple of String arrays to our resources (and define a class names ListViewFragment, which you can see in the source, but which we won’t discuss here):
<?xml version="1.0" encoding="utf-8"?>
If we run this we can see that our navigation list gets populated accordingly:
Update: As per Jake Wharton’s comments, the source code has been updated accordingly. Thanks, Jake!
In the next article we’ll take this a little further and show some alternative approaches that we can use. The source code for this article is available here.
© 2013 – 2014, Mark Allison. All rights reserved.
Adapters – Part 1 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.