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
341
votes
25 answers

How to set selected item of Spinner by value, not by position?

I have a update view, where I need to preselect the value stored in database for a Spinner. I was having in mind something like this, but the Adapter has no indexOf method, so I am stuck. void setSpinner(String value) { int pos =…
Pentium10
  • 204,586
  • 122
  • 423
  • 502
201
votes
25 answers

Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"

What I want to do: run a background thread which calculates ListView contents and update ListView partially, while results are calculated. What I know I have to avoid: I cannot mess with ListAdapter contents from background thread, so I inherited…
tomash
  • 12,742
  • 15
  • 64
  • 81
185
votes
4 answers

ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView

I am attempting to create a custom Adapter for my ListView since each item in the list can have a different view (a link, toggle, or radio group), but when I try to run the Activity that uses the ListView I receive an error and the app stops. The…
Dan
  • 3,884
  • 3
  • 27
  • 32
176
votes
2 answers

Difference between the Facade, Proxy, Adapter and Decorator design patterns?

What is the difference between the Facade, Proxy, Adapter, and Decorator design patterns? From a generic point of view, such patterns seem to do the same thing, that is: wrap an API and provide access to it. How to distinguish these patterns? How to…
user310291
  • 36,946
  • 82
  • 271
  • 487
171
votes
13 answers

When to use the Bridge pattern and how is it different from the Adapter pattern?

Has anyone ever used the Bridge pattern in a real world application? If so, how did you use it? Is it me, or is it just the Adapter pattern with a little dependency injection thrown into the mix? Does it really deserve its own pattern?
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
171
votes
11 answers

Difference between Bridge pattern and Adapter pattern

What is the difference between the Bridge and Adapter patterns?
Firoz
  • 7,224
  • 10
  • 41
  • 56
161
votes
7 answers

What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

I'm curious about the purpose of the methods getItem and getItemId in the class Adapter in the Android SDK. From the description, it seems that getItem should return the underlying data. So, if I have an array of names ["cat","dog","red"] and I…
oneporter
  • 3,014
  • 3
  • 23
  • 15
161
votes
16 answers

What is the difference between the Facade and Adapter Pattern?

I've been reading both definitions and they seem quite the same. Could anyone point out what are their differences?
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
151
votes
17 answers

Android: How to bind spinner to custom object list?

In the user interface there has to be a spinner which contains some names (the names are visible) and each name has its own ID (the IDs are not equal to display sequence). When the user selects the name from the list the variable currentID has to be…
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
134
votes
10 answers

What's the role of adapters in Android?

I want to know when, where and how adapters are used in the context of Android. The information from Android's developer documentation was insufficient for me and I'd like to get a more detailed analysis.
Robin
  • 6,879
  • 7
  • 37
  • 35
129
votes
9 answers

Call Activity method from adapter

Is it possible to call method that is defined in Activity from ListAdapter? (I want to make a Button in list's row and when this button is clicked, it should perform the method, that is defined in corresponding Activity. I tried to set…
user1602687
  • 1,477
  • 3
  • 14
  • 13
100
votes
23 answers

Bridged networking not working in Virtualbox under Windows 10

I just upgraded my laptop from Windows 7 to Windows 10 and found that I am unable to start Virtualbox VMs configured with a bridged adapter. See the configuration below:
J.Wang
  • 1,241
  • 3
  • 13
  • 17
95
votes
15 answers

adapter-Any real example of Adapter Pattern

I want to demonstrate use of Adapter Pattern to my team. I've read many books and articles online. Everyone is citing an example which are useful to understand the concept (Shape, Memory Card, Electronic Adapter etc.), but there is no real case…
AksharRoop
  • 2,263
  • 1
  • 19
  • 29
78
votes
10 answers

Clear listview content?

I have a little problem with ListView. How do I clear a ListView content, knowing that it has a custom adapter? edit - the custom adapter class extends BaseAdapter, it looks like this: import android.app.Activity; import…
Mohamed Gallah
  • 1,158
  • 1
  • 10
  • 16
76
votes
9 answers

How to start Activity in adapter?

I have a ListActivity with my customized adapter and inside each of the view, it may have some buttons, in which I need to implement OnClickListener. I need to implement the OnClickListener in the adapter. However, I don't know how to call the…
justicepenny
  • 2,004
  • 2
  • 24
  • 48
1
2 3
99 100