For Android Developers, the adapter mode is quite familiar. Many applications use the adapter mode, such as listview, directly or indirectly every day.
Listview is used to display list data. However, as a list dataset, there are many forms, including array and cursor. We need the corresponding adapter as a bridge, process the corresponding data (and form the view required by listview ).
Only by defining these adapter interfaces and adapter classes can we make our data simple, flexible, and correctly displayed to the adapterview implementation class.
Adapter mode, adapter pattern, brave adaptation, a large amount of resources can be reused.
1. Intention
Adapter mode, which converts the interface of a class into another interface that the client expects, so that the two that do not match and cannot work together can work together.
The adapter mode consists of the class adapter mode and the Object Adapter mode.
For the class adapter mode, because of Java's single inheritance, if one class is inherited and the other is only an interface, you need to manually implement the corresponding method.
Popular Words:Class adapter Mode Object Adapter Mode Default adapter Mode Source class Target Interface
2. structure chart andCode
To be concise and straightforward, I omitted other related adapters, taking these two adapters as an example.
As the client, listviews uses the Target Interface (Target Interface) as the listadapter, which includes several basic methods, such as getcount (), getitem (), and getview, to be compatible with list <t> and cursor data types as data sources, we define two adapters to adapt to them: arrayadapter and cursoradapter. To put it bluntly, these two adapters are compatible with the data source for the target interface.
This is the adapter mode.
Here, baseadapter implements the isempty () method, so that the subclass does not need to implement this method after inheriting the baseadapter. This is the default adapter, which is also the most obvious benefit of the default adapter.
The following is an example of the simplest method. The listadapter interface is as follows (for simplicity, I omitted the inherited self-adapter Interface ):
Public interface listadapter {public int getcount (); object getitem (INT position); long getitemid (INT position); view getview (INT position, view convertview, viewgroup parent ); boolean isempty ();}
Abstract class baseadapter. I omit other code and only list two methods as a illustration:
Public abstract class baseadapter implements listadapter, spinneradapter {//...... public View getdropdownview (INT position, view convertview, viewgroup parent) {return getview (Position, convertview, parent);} public Boolean isempty () {return getcount () = 0 ;}}
Arrayadapter encapsulates list <t> into listadapter to meet the following requirements:
Public class arrayadapter <t> extends baseadapter implements filterable {private list <t> mobjects; // I only list this constructor, int textviewresourceid, t [] objects) {Init (context, textviewresourceid, 0, arrays. aslist (objects);} private void Init (context, int resource, int textviewresourceid, list <t> objects) {mcontext = context; minflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); mresource = mdropdownresource = resource; mobjects = objects; // The referenced object is also an expression of mfieldid = textviewresourceid;} public int getcount () {return mobjects. size ();} public t getitem (INT position) {return mobjects. get (position);} public view getview (INT position, view convertview, viewgroup parent) {return createviewfromresource (Position, convertview, parent, mresource );} //......}
In this way, we successfully passed the list <t> as the data source to the listview as the desired target interface. Similarly, the cursoradapter is the same, so we will not write the specific code.
The adapter itself is not difficult, but provides a common mode for understanding incompatibility issues.
When to use the adapter mode, there are three situations:
(1) You want to use an existing class, and its interfaces do not meet your requirements. This is common when dealing with old systems.
(2 ). you want to create a reusable class that can work together with other unrelated classes or unpredictable fatigue. This is a situation that Android Developers often encounter: we often customize a new adapter.
(3) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.
3. Results
1. Structural Model
2. the Object Adapter is discussed above. In addition to implementing the target port, the class adapter also needs to implement the source class that you want to be compatible with, so that you can write less lines of code, however, from the perspective of better combination than inheritance, it is generally not so clean.
3. Two-way or even multi-direction adaptation to the same source for the same adapter (that is, the same object), so that it can be applied to two or more customer calls.