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 website 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,
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,
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,
CursorAdapter
setViewText
and setViewImage
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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With