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
66
votes
2 answers

OpenVPN failed connection / All TAP-Win32 adapters on this system are currently in use

^^ i get this error if i would connect to any OpenVPN Network. All TAP-Win32 adapters on this system are currently in use. Here is a Screenshot. -> http://www.abload.de/image.php?img=openvpn_failbhjde.jpg What i did before i post this? Removing…
PatrickB
  • 3,225
  • 5
  • 31
  • 55
65
votes
14 answers

Remove all items from RecyclerView

I am trying to remove all the elements from my RecyclerView in my onRestart method so the items don't get loaded twice: @Override protected void onRestart() { super.onRestart(); // first clear the recycler view so items are not populated…
user2456977
  • 3,830
  • 14
  • 48
  • 87
63
votes
4 answers

Filtering ListView with custom (object) adapter

I'm trying to implement filtering of a ListView which is uses a custom object adapter, but I can't find any useful samples. The included code is very simplified, so no- keep in mind I can't use an regular ArrayAdapter. I have a EditText above the…
GeirFrimann
  • 631
  • 1
  • 6
  • 4
58
votes
9 answers

AutoCompleteTextView item selection programmatically

I have an AutoCompleteTextView that is filled with cities from an sqlite database that calls an AsyncTask on item click, recently I added an option to detect my location using the gps, so the problem is I can detect the city (i.e Beirut) and set the…
Hussein Yassine
  • 595
  • 1
  • 4
  • 6
55
votes
3 answers

What are Containers/Adapters? C++

What are containers/adapters? I have basic knowledge of C++ and its sub-topics like (class/templates/STL). Can anyone please explain in layman's language and give me a practical example of the application of containers/adapters?
Pavitar
  • 4,282
  • 10
  • 50
  • 82
53
votes
3 answers

GetView Vs. BindView in a custom CursorAdapter?

So, I'm watching this video http://www.youtube.com/watch?v=N6YdwzAvwOA and Romain Guy is showing how to make more efficient UI adapter code using the getView() method. Does this apply to CursorAdapters as well? I'm currently using bindView() and…
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
52
votes
6 answers

Logcat error: "addView(View, LayoutParams) is not supported in AdapterView" in a ListView

I'm doing an application for Android and something I need is that it shows a list of all files and directories in the SD Card and it has to be able to move through the different directories. I found a good tutorial in anddev. I modified a few things…
Alberto Elias
  • 891
  • 2
  • 8
  • 17
42
votes
3 answers

Sync Adapter without Account

I need to fetch some data over the cloud from my app. I've watched the google IO video on RESTful android apps @ http://www.youtube.com/watch?v=xHXn3Kg2IQE&t=43m58s It recommends in the final slides to use a SyncAdapter to integrate with the Android…
siamii
  • 23,374
  • 28
  • 93
  • 143
40
votes
8 answers

Text on spinner is white on a white background

The text on my spinners is white, and I have no idea why. This is my xml, nothing special And…
Robby Smet
  • 4,649
  • 8
  • 61
  • 104
40
votes
4 answers

can we call startActivityForResult from adapter?

is it possible to have method onActivityResume within adapter & call startActivityForResult?
napster
  • 687
  • 1
  • 8
  • 18
38
votes
6 answers

ClassCastException with ListView when executing notifyDataSetChanged

I have added a view to the header of listVivew, View TopSearch = (View) View.inflate(this, R.layout.search, null); lv.addHeaderView(TopSearch, null, false); And everything is fine until I try to execute (when data changes)…
bobetko
  • 5,019
  • 14
  • 58
  • 85
37
votes
9 answers

when do we need Adapter pattern?

When do we need to go for Adapter pattern? If possible give me a real world example that suits that pattern.
brainless
  • 5,698
  • 16
  • 59
  • 82
36
votes
12 answers

How to remove listview all items

In my app, if you click on a button then i want to remove all the listview items. Here, i am using the base adapter for adding the items to the list view. How can i remove the listview items dynamically.
naresh
  • 10,332
  • 25
  • 81
  • 124
36
votes
4 answers

GridView inside Expandable list in android

I want to put an gridview with images inside an expandable list...I've already make that, but the gridview don't show all item... How can i make my expandable list child adapt to the gridview size? LIST ADAPTER public class CustomListAdapter extends…
Zasuk
  • 750
  • 1
  • 7
  • 14
36
votes
6 answers

Adapter Pattern: Class Adapter vs Object Adapter

I have a few questions about the Adapter pattern. I understand that the class adapter inherits from the adaptee while the object adapter has the adaptee as an object rather than inheriting from it. When would you use a class adapter over an object…
Silverbolt
  • 7,093
  • 5
  • 20
  • 18