Questions tagged [adapter]

Use this tag for questions relating to the Adapter design pattern, one of the Gang of Four's structural design patterns. Also consider using the [design-patterns] tag and a programming language tag if applicable.

According to the GoF book (page 139) the purpose of the Adapter design pattern is to,

Convert the interface of a class into another interface clients expect. The adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

There are two different versions of the Adapter design pattern (page 141).

  1. A class adapter uses multiple inheritance to adapt one interface to another. 2. An object adapter relies on object composition.

There are three scenarios where the Adapter design pattern is applicable (page 140).

  1. you want to use an existing class, and its interface does not match the one you need. 2. you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces. 3. (object adapter only) you need to use several existing subclasses, but it's unpractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

There are different consequences of applying the Adapter design pattern depending on which version is used (page 142).

Class and object adapters have different trade-offs. A class adapter

  • adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won't work when we want to adapt a class and all its subclasses.
  • lets Adapter override some of Adaptee's behavior, since Adapter is a subclass of Adaptee.
  • introduces only one object, and no additional pointer indirection is needed to get to the adaptee.

An object adapter

  • lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
  • makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.

For details about the structure and implementation of the Adapter design pattern, see the following online resources.

Note the tag encompasses this pattern as well as the other 22 patterns from the GoF book. Consider using any of these tags in combination, as applicable.

5035 questions
34
votes
7 answers

Best place to addHeaderView in ListFragment

I'm having some trouble setting up my custom header in my list. I'm creating a ListFragment with a custom adapter. I have the list working fine, but I'm trying to figure out where in the lifecycle of a Fragment to attach the header. I know the…
brockoli
  • 4,516
  • 7
  • 38
  • 45
33
votes
8 answers

OnItemClickListener doesn't work with ListView item containing button

I have ListView with custom Adapter which supplies View to ListView in this way: public View getView(int position, View convertView, ViewGroup parent) { RelativeLayout.LayoutParams lineParams; RelativeLayout line=new…
Barmaley
  • 16,638
  • 18
  • 73
  • 146
33
votes
8 answers

How to sort RecyclerView item in android

I'm working on a android chatting application. When I called my api it returns me the chat list sorted by a user_id. But what I need to do is serialized by message_id as I want to show last message first.Here is my onBindViewHolder method in which i…
Tanvir Durlove
  • 768
  • 2
  • 9
  • 21
33
votes
7 answers

RecyclerView Adapter notifyDataSetChanged stops fancy animation

I am building a component based on RecyclerView, allowing user to reorder items by drag and drop. Once I am on the DragListener side, I need the position it has in the adapter in order to perform correct move, but I only have access to the view. So…
elgui
  • 3,303
  • 4
  • 28
  • 37
31
votes
5 answers

Understanding Adapter Pattern

I am trying to understand Adapter pattern and its use in real world. After going through various articles on internet and www.dofactory.com, I created this sample code. I just want to know whether my understanding is correct. In the example below I…
pradeeptp
  • 2,131
  • 6
  • 29
  • 39
30
votes
3 answers

What is the exact difference between Adapter and Proxy patterns?

As I understood both Adapter and Proxy patterns make two distinct/different classes/objects compatible with each for communication. And both of them are Structural patterns. I am getting that both of them are pretty much similar with each other. Can…
29
votes
8 answers

Android: notifyDataSetChanged(); not working

I have a database in a server and from a Tablet I take some values from one table in the database. I load this information correctly into a list but I would like to know why when there is a change, nothing happens even if I use…
Katherine99
  • 980
  • 4
  • 21
  • 40
28
votes
3 answers

ViewHolder pattern correctly implemented in custom CursorAdapter?

Here is my custom CursorAdapter: public class TasksAdapter extends CursorAdapter implements Filterable { private final Context context; public TasksAdapter(Context context, Cursor c) { super(context, c); this.context =…
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
27
votes
4 answers

Difference between object adapter pattern and class adapter pattern

How to decide when to use object adapter and when to use class adapter? Problem statement: To create social networking web site and provide import functionality from facebook, google plus and orkut. I am unable to decide whether to use object…
Tilak
  • 30,108
  • 19
  • 83
  • 131
25
votes
2 answers

JAXB: Isn't it possible to use an XmlAdapter without @XmlJavaTypeAdapter?

Can't I register a bunch of XmlAdapters to Marshaller|Unmarshaller so that I wouldn't need to specify @XmlJavaTypeAdapter on each filed, whose type isn't natively JAXB-supported? I find it somewhat redundant. BTW, someMarshaller.setAdapter(...) seem…
23
votes
1 answer

What's Adapter.getItem() for?

I am writing a custom adapter for use with a ListView. The Adapter interface includes a getItem() method which returns, according to the docs, an Object as the data item associated with the specified position in the data set. What's this object…
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
23
votes
9 answers

What is an adapter class?

I googled and investigated, but I still need some clarification: Are an adapter class and a controller class similar? If not, in what way they do differ? Kindly explain.
Naruto
  • 9,476
  • 37
  • 118
  • 201
22
votes
6 answers

Why set setBackgroundColor is not working in my custom listView

I have a custom listView. The main layout xml is something like this:
Desenfoque
  • 804
  • 2
  • 11
  • 30
22
votes
2 answers

Setting network adapter metric priority in Windows 7

I'm having an issue on Windows 7 - if I have my Ethernet cable plugged in, Windows will default to using my WiFi network adapter. I would prefer that Windows default to my Ethernet connection. In order to resolve this, I have to manually…
Michael
  • 415
  • 2
  • 5
  • 7
22
votes
2 answers

Whats the difference between an adapter and a network interface?

Having a hard time understanding the MSDN documentation on the IP Helper Functions. Whats the difference between an adapter and a network interface?
unixman83
  • 9,421
  • 10
  • 68
  • 102