A list view is a general container view in Android. It can be compared to a one dimensional array or list in data representation. However, a list view is far more flexible than a list of simple values. A list view can be seen as a container that can contain any number of element views. Furthermore, element views do not have to be the same, and each element view can be fully customized as it own view container (containing sub-views).
A list view is represented by a ListView object.
ListView is a subclass of a View. However, it is also a subclass of an AdapterView. This makes ListView a little special due to the use of an adapter.
An adapter is a mechanism that bridges two constructs. In the case of ListView, an interface ListAdapter defines what an object must implement in order to be passed as a parameter.
Note that most of the methods to be implemented come from the super-interface android.widget.Adapter.
An adapter is an object that helps translate operations across two constructs. In this case, a ListAdapter object bridges what a ListView object does and what an underlying data representation does. Most of the methods of ListAdapter (and its super-interfaces) are intuitive, but a few requires some extra explanation.
getItemId needs to return a unique identifier (as a long integer) of the given row number. This is intended for database related underlying data that is behind a list view. It provides a convenient method to associate a row (element) of a list view to an internal ID to facilitate look up when an item of a list view is clicked.
getItemId has to be implemented. In the simplest case, it can just return the parameter itself.
getViewTypeCount indicates the number of types of views that can be returned by getView. If there is only one type, this method should return 1.
getItemViewType is a matching method that indicates the enumerated type of the item on a particular row (as the parameter). This number must be zero-oriented and up to the value returned by getViewTypeCount minus one. The actual enumeration is entire app dependent and should be specified by app code.
getView is the main mechanism to return a View object as an item in a ListView.
The getView method returns a View object. It is given three parameters: the position, an old View object to reuse (if any), and the parent View object.
There are a few important points here. The second parameter of getView may be non-null to begin with. Whether it can be reused depends on whether it is the right type or not.
The best way to check is to test whether it is an instance of a particular subclass using the instanceof operator. However, this requires the ability to create a new View subclass object from a layout. This can be easily done by modifying the XML file of a layout. Change the top level element from a layout class to a custom class. For example, change it from LinearLayout to com.example.testprogram.MySpecialView.
This top element determines the class of the object inflated from the XML file.
With a custom subclass associated with custom view, then it is easier to use instanceof to make sure a supplied View object of getView is of the right type before reusing it.
Note that the custom subclass must have a constructor that takes a Context and a AttributeSet parameter in order for this method to work.