Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Adapter pattern in Android

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to implement the adapter pattern in Android. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. Intention

The adapter pattern transforms the interface of one class into another interface expected by the client, so that the two classes that do not match and cannot work together can work together.

The adapter pattern is divided into class adapter pattern and object adapter pattern.

With regard to the class adapter pattern, because of java's single inheritance, if you inherit a class, the other can only be an interface, and you need to implement the corresponding methods manually.

Hot words: adapter pattern of class adapter pattern object adapter pattern default adapter pattern source class target interface

two。 Structure diagrams and codes

For the sake of conciseness and directness, I omitted the other adapters and only took these two adapters as an example.

ListViews as a client, the target interface (target interface) he needs is ListAdapter, including getCount (), getItem (), getView () and other basic methods. In order to be compatible with data types such as List,Cursor as data sources, we specifically define two adapters to adapt to them: ArrayAdapter and CursorAdapter. These two adapters, to put it bluntly, are compatible with the data source for the target interface.

This is the adapter pattern.

Where BaseAdapter implements methods such as isEmpty (), so that subclasses do not need to implement this method after inheriting BaseAdapter, which is the default adapter, which is one of the most obvious benefits of the default adapter.

Let's take some of the simplest methods as an example, and the ListAdapter interface is as follows (for simplicity, I omitted inheriting from the 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 ();}

For the abstract class BaseAdapter, I omitted the other code and listed only two methods as an 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 into ListAdapter implementation, which satisfies the call of ListView:

Public class ArrayAdapter extends BaseAdapter implements Filterable {private List mObjects; / / I only list this one constructor. If you know what this means, public ArrayAdapter (Context context, int textViewResourceId, T [] objects) {init (context, textViewResourceId, 0, Arrays.asList (objects));} private void init (Context context, int resource, int textViewResourceId, List objects) {mContext = context MInflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); mResource = mDropDownResource = resource; mObjects = objects; / / reference object, which also means that combination is superior to inheritance 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);} /.}

We have successfully passed List as a data source to ListView as the target interface that ListView wants, just like CursorAdapter, so we don't have to write specific code.

The adapter itself is not difficult, but it provides an idiomatic pattern for solving incompatibility problems.

There are probably three situations about when to use the adapter pattern:

(1)。 You want to use an existing class, and its interface does not meet your requirements, which is more common when dealing with old systems.

(2)。 You want to create a reusable class that works with other unrelated classes or unforeseen tiredness, which is what we android developers often encounter: we often customize a new Adapter.

(3)。 You want to use some existing subclasses, but it is not possible to subclass each to match their interface, and the object adapter can adapt to its parent interface.

That's all of the article "how to implement the Adapter pattern in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report