47. Overview of adapters

Lecture



In this lesson:

- we understand adapters

In the last lessons we used adapters and now we can well formulate what it is. The adapter is the bridge between the data set and the object using the data. The adapter is also responsible for creating a View component for each unit of data from the set.

I remember by myself that it’s not easy for a newbie to understand the adapters. It seems that they are a bunch and it is absolutely incomprehensible which one and where is used. And talking about it without practice is pointless. Therefore, in my lessons, I first considered a couple of examples so that a pattern of use emerged in my head: a data set - an adapter - an object. And now it will be easier to make out what other adapters are there and how they differ.

In the examples we reviewed were:
- ArrayAdapter adapter and object to display ListView data
- adapter SimpleExpandableListAdapter and object ExpandableListView .

I will try to review the adapters in this material and show how they differ from each other. The material will be periodically updated and replenished. In the meantime, I have added a java hierarchy of interfaces and adapter classes. I specify the type in brackets: I is the interface, AC is the abstract class, C is the class. Lines are inheritance. Read should be from top to bottom.

  47. Overview of adapters

Let's look at it in order, why it is all necessary.

Interface Adapter. Describes the basic methods that the adapters should contain: getCount, getItem, getView, etc.

ListAdapter interface. This interface must be implemented by the adapter that will be used in the ListView (setAdapter method). Contains a description of the methods for working with separators (separator) list.

SpinnerAdapter interface. Adapters that implement this interface are used to build a Spinner (drop-down list or drop-down). Contains the getDropDownView method, which returns a drop-down list item. On the official site there is an example of use.

WrapperListAdapter interface. Adapters that inherit this interface are used to work with nested adapters. Contains the getWrappedAdapter method, which allows you to pull nested from the main adapter. I will explain a little further.

The HeaderViewListAdapter class. Ready adapter for working with Header and Footer. Inside it contains another adapter (ListAdapter), which can be obtained using the above method getWrappedAdapter from the WrapperListAdapter interface.

Abstract class BaseAdapter. It contains some of its methods and implements the methods of interfaces that inherits, but not all. Leaves his successors on mandatory implementation methods: getView, getItemId, getItem, getCount from ListAdapter. Those. If you want to create your own adapter, this class suits you.

The ArrayAdapter <T> class. Ready adapter that we have already used. Accepts a list or an array of objects as input, iterates through it and inserts a string value into the specified TextView. In addition to the inherited methods, it contains methods for working with the data collection — add, insert, remove, sort, clear, and the setDropDownViewResource method for defining a layout resource for displaying drop-down list items.

Class SimpleAdapter. Also ready to use adapter. Accepts a list of Map objects, where each Map object is a list of attributes. In addition, two arrays are taken as input - from [] and to []. In to specify the id of the screen elements, and in from the names (key) of the Map objects, the values ​​of which will be inserted into the corresponding (from from) screen elements.

Those. SimpleAdapter is an expanded ArrayAdapter. If you make a ListView and you have each item in the list not one TextView, but several, then you use a SimpleAdapter. In addition to the inherited methods, SimpleAdapter contains methods for filling View-elements with values ​​from Map - setViewImage, setViewText, setViewBinder. Those. We see that he can work not only with text, but also with images. The setViewBinder method is a great thing, it allows you to write your parser of values ​​from Map to View-elements and the adapter will use it. This we will discuss in more detail in further lessons.

Also contains an implementation of the setDropDownViewResource method.

Abstract class CursorAdapter. It implements the abstract methods of the BaseAdapter class, contains its own methods for working with the cursor and leaves to the heirs methods for creating and filling the View: newView, bindView.

Abstract class ResourceCursorAdapter. Contains methods for configuring layout-resources used by the adapter. Implements the newView method from the CursorAdapter.

Class SimpleCursorAdapter. Ready adapter, similar to SimpleAdapter. Only it uses not a set of Map objects, but a Cursor, i.e. rowset with fields. Accordingly, in the from [] array, you enter the names of the fields whose values ​​you want to pull into the appropriate View from the to array.

Contains the convertToString method, which returns the string value of a column, which is set by the setStringConversionColumn method. Or, you can set your converter using the setCursorToStringConverter method and the adapter will use it when you call convertToString. In this converter, you yourself realize what it will return.

In total, we received 4 ready-made adapters: HeaderViewListAdapter, ArrayAdapter <T>, SimpleAdapter, SimpleCursorAdapter. Which one to use is up to you. If you have an array of strings, then without hesitation, take an ArrayAdapter. If you work with a database and there is a cursor, the data from which must be listed, use the SimpleCursorAdapter.

If these adapters do not suit you, there is a set of abstract classes and interfaces that we can inherit and implement in our classes to create our own adapter. Yes, and ready-made adapters can always inherit and make their own implementation of methods.

In addition to this hierarchy, there is almost similar to it. It contains adapters for working with the tree - ExpandableListView . I will not paint them here, in general, they are similar to the objects we have already examined. But there is a difference in the fact that the data here are not single-level, but are divided into groups and elements.

  47. Overview of adapters

Here we see one ready-made adapter. SimpleExpandableListAdapter - works with Map collections.

If you are interested in examples of the work of an adapter - write on the forum in the thread of this lesson (link at the bottom of the page). The next few lessons will be devoted to this topic.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)