Lists Presentation

Lists Presentation


  • Use an ArrayList to store multiple strings
  • Display many items in a list
  • Understand the adapter pattern for connecting lists of items to data
  • Customize a ListView item display


  • Data structure to hold multiple data objects of the same type
  • Static arrays in Java can't change the number of items
    String[] names = new String[] { "Adam, "Betty", "Carl", "Donna" };
    String singleName = names[3]; // This will be Carl
    names[0] = "Alicia"; // Replace Adam with Alicia
  • Dynamic arrays more versatile
    ArrayList<String> names = new ArrayList<String>();
    String singleName = names.get(1); // This will be Betty
    names.set(0, "Adele"); // Replace Adam with Adele

Lists on screen

  • Lists of items displayed in the user interface are of high importance and use on mobile devices
  • Both Android and iOS have specific feature for dealing with virtualizing lists
  • Virtual lists
    • Not efficient on mobile to have all data items in a list/table transformed into visual elements in the user interface
    • A virtual list is a feature where the system handles scrolling and knowing exactly which data items is enough to be visible at any particular point in time
    • Items not visible any more (from scrolling etc) are automatically freed from memory
    • Visual elements can be reused when scrolling to new data items so they don't have to be recreated repeatedly

Objects for virtual lists

  • ListView
    • The visual element in the user interface that handles display, scrolling etc
    • Knows which items in the list are currently visible on screen
    • Never extended or customized by you
  • Data source - for example an ArrayList<String>
    • Some sort of data storage mechanism that is capable of storing multiple items of data
    • Never handled directly by the ListView
    • Use a built in data storage class/concept
  • Adapter - connects the ListView to the data
    • Abstraction of the data source so that the same ListView can handle many different types of data storage
    • ListAdapter base class is extended into specific adapters for some types of data storage
    • Often extended and customized by you to give you full control of how your data items are displayed


  • Adapters sit between a ListView for presentation and some type of array data storage
  • ArrayAdapter is a specific Adapter that can also store the actual items
    • However, we'll focus on a single use case where the ArrayAdapter wraps an ArrayList
  • When creating an ArrayAdapter you specify
    • The context for finding resources
    • The layout XML that will visualize a single data item
    • The ArrayList source that will store all data items
  • Example
    ArrayList<String> data = new ArrayList<String>();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, data);


  • ListView handles the displays of data items, using an Adapter as the interface to the data
  • Add ListView to layout XML file
    <ListView android:id="@+id/list"
      android:layout_width="match_parent" android:layout_height="match_parent" />
  • Get a reference to it in Java code and set its Adapter
    ListView list = (ListView) findViewById(;

Built-in resources for ListAdapters

  • Specify a layout xml file
    • android.R.layout.simple_list_item_1
      • Built-in layout with a single TextView
    • android.R.layout.simple_list_item_2
      • Built-in layout with one larger and one smaller TextView below each other
  • Specify id of TextView inside of layout
      • Single TextView in android.R.layout.simple_list_item_1 or the larger first TextView in android.R.layout.simple_list_item2
      • Second smaller TextView in android.R.layout.simple_list_item_2

Adapter choices

  • Data in an array, like ArrayList<String>
    • Use an ArrayAdapter<String>. Can also handle the storage, if you don't want to have a separate object for storing the individual items in the array.
  • Data in an ArrayList of HashMaps, like ArrayList<HashMap<String, String>>
    • Use a SimpleAdapter. Can map values from key of String in HashMap to in layout.
  • Data in a SQLite database
    • Use a SimpleCursorAdapter.

Customized list items

  • When you need more control than showing a single String
  • Create your own class derived from ArrayAdapter
  • Override getView method to build the user interface for each item
    • Use a pattern for dealing with reusable item views and the whole virtualization that the ListView and Adapter provides
  • Access and modify whatever you like in each item

Custom item XML layout

  • Example (not all attributes shown for clarity)
      <ImageView android:id="@+id/icon" />
      <TextView android:id="@+id/title" />
      <TextView android:id="@+id/hours" />

Custom Adapter class

  • Inherit from an existing Adapter
    • Define a constructor
    • Override the getView method that creates each layout item for the list
  • Your custom class
    class MyAdapter extends ArrayAdapter<String> {
        public MyAdapter(Context context, ArrayList<String> data) {
            super(context, R.layout.item, data);
        public View getView(int position, View convertView, ViewGroup parent) {

Custom layout items

  • Inflate the item layout
    public View getView(int position, View view, ViewGroup parent) {
      if (view == null) {
        view = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null);
      String item = getItem(position);
      TextView text = (TextView) view.findViewById(;
      return view;

ListView events

  • Events fired for the list item as a whole
    • Create and specify in onCreate when activity starts
  • OnItemClickListener
    • When user touches on an item in the list
  • OnItemLongClickListener
    • When user touches and holds on for a period of time, on an item in the list
  • Events fired from specific views inside the list item
    • Create and specify inside of getView

ListView event handler

  • Implement event handler
    ListView list = (ListView) findViewById(;
    list.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  • Arguments
    • AdapterView<?> is the adapter for the ListView
    • View is the visual element container for the particular item that was clicked
    • int is the position of the item that was clicked (first is zero)
    • long is the identifier of a SQLite database table row, if a CursorAdapter was used

Updating the data

  • The data source may change as much as needed
  • Tell adapter about change and update the list
    • Call notifyDataSetChanged on the adapter
    • Store the adapter as a class member, or retrieve it from the list
  • Example
    ListView list = (ListView) findViewById(;
    ArrayAdapter<String> adapter = (ArrayAdapter<String>) list.getAdapter();