An adapter is used to bind data to a view. See AdapterView:
An AdapterView is a view whose children are determined by an Adapter.
Several layout views derive from AdapterView like GridView, ListView, and Gallery.
Of course, you generally don't use AdapterView
and Adapter
directly, but rather use or derive from one of their subclasses. The subclasses of Adapter may add additional functionality that change how to you should bind data to view.
BaseAdapter
is an abstract base class for the Adaptor interface to simplify implementing adapters. You could implement your own, but the framework provides some pretty flexible adapters already. Some popular adapters are:
ArrayAdapter,
- binds an array of data to a view
- override
getView()
to inflate, populate, and return a custom view for the given index in the array. The getView()
method includes an opportunity reuse views via the convertView
parameter.
CursorAdapter,
- binds data from a cursor (like a database cursor) to a view
- abstract so you don't use it directly, use a subclass or derive your own
- implement the abstract method
newView()
to inflate, populate, and return the desired view for the current cursor position and implement the abstract method bindView
to populate an existing view that is being reused..
SimpleCursorAdapter,
- a concrete implementation of
CursorAdapter
- it can take a row layout and a mapping of cursor columns to row layout widgets
- supports text and images, but can customize using
setViewText
and setViewImage
- can support other types and can customize bindings through a hook: clients implement the
SimpleCursorAdapter.ViewBinder
interface with a setViewValue()
method to inflate, populate, and return the desired view for a given row (current cursor state) and data "column". This method can define just the "special" views and bindings, but still defer to SimpleCursorAdapter's standard behavior for the "normal" bindings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…