I'm trying to set up a an image GridView Layout, and this involves deriving a new class from the BaseAdapter class. I have been using the tutorial on the webs开发者_开发技巧ite developer.android.com, but I still don't quite understand what it means. Could someone please explain to me what exactly is a BaseAdapter? I don't understand the definition provided by the Android developers website.
Thanks
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. ThegetView()
method includes an opportunity reuse views via theconvertView
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 methodbindView
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
andsetViewImage
- can support other types and can customize bindings through a hook: clients implement the
SimpleCursorAdapter.ViewBinder
interface with asetViewValue()
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.
http://developer.android.com/resources/tutorials/views/hello-gridview.html
The GridView is a subclass of a type of view known as an AdapterView. These generally contain a number of smaller Views, but rely on an Adapter to give them those Views. The BaseAdapter class is one which you extend in order to override the methods which tell the AdapterView (your GridView) what to display. The most important method to override is the getView() method, in which you return the View to display at a particular position on the grid.
While this setup is a bit confusing and complicated, the developers of Android chose this because of all the optimisations it allowed. A lot of optimisation for AdapterViews is obtained by reusing Views instead of creating new ones in the getView method. This is what the convertView argument you can see in the getView method is for. If convertView == null, then the method needs to create a new View object and return that, an expensive operation. If it is not null, then the Adapter can reuse the View, simply changing the image in that tutorial example.
精彩评论