ANDROID LISTS
Android Lists (Introduction) Items can be placed in a list from which the user can scroll through the items The selection of a list item might (and often does) start another activity or fragment Or display detail data Adapters manage the data model We connect the list to an array or other data type
Adapter Pattern Adapters are just another design pattern The adapter design pattern makes incompatible things compatible Adapters are structural patterns Intent: Convert the interface of a class into another one Wrap a class with a new interface http://sourcemaking.com/design_patterns/adapter
Arrays as Resources We can create array resources <string-array> is an array of strings <integer-array> is an array of integers
Arrays as Resources (Example)
Reading an Array Resource Remember that getResources() is used to get a resource getStringArray() gets a string resource array getIntArray() gets an integer resource array http://developer.android.com/reference/android/content/res/Resources.html
Reading an Array Resource (Example) Get the string array named adobe_products and store the result in the String array variable adobe_products Use R.array.array_name String[] adobe_products = getResources().getStringArray( R.array.adobe_products);
Creating the ArrayAdapter (1) The constructor we will use accepts 4 arguments: The first contains a reference to the current context (this) The second contains the resource ID for a layout (R.layout.list_item) The third contains the id of the TextView within the layout to be populated The last contains the array of objects
Creating the ArrayAdapter (2)
Working with the Lists (Method 1) Derive an Activity from ListActivity Use this method when implementing a full screen list ListActivity manages the complexities of the list itself Such as providing a default full screen layout It’s possible to create a custom layout though
Working with the Lists (Method 2) The ListView widget is used when you need a custom screen layout There are requirements The layout MUST contain a ListView object
Working With Lists (Method 1)
The ListView (Introduction) The ListView is a view group that displays a list of scrollable items The ExpandableListView class supports a grouping of items Items are added using an adapter that pulls content from a source (commonly called the data model) An array (ArrayAdapter) Or database query (CursorAdapter)
Referencing the ListView The getListView method applies to the ListActivity class. It gets the activity’s ListView widget The control is built-in to ListActivity
Wiring onItemClick The ListView has an OnItemClickListener
Wiring onItemClick (Example)
Implementing the ListView In the layout file, you need to create the ListView itself
Implementing the ListView Next, wire he ListView to the Adapter Note here we are using an inline array rather than a resource