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
1
vote
1 answer

Adapter/Wrapper and equal references

I want to create a wrapper class for another type. This works fine until the point where it's necessary that (reference)-equal objects need to have (reference)-equal wrappers. An example: public interface ITest { T GetInstance(bool…
Wolfgang Kluge
  • 895
  • 8
  • 13
1
vote
4 answers

Where to place adapter.notifyDataSetChanged(); when using Volley

This code runs fine, displaying my JSON response into an Arraylist. The problem is, that each time I click on the button to display the list again, it simply duplicates the previous JSON response, resulting in a list of repeated results. How can I…
AnonProg
  • 117
  • 1
  • 4
  • 15
1
vote
1 answer

Can we consume multiple message from JMS queue by single read?

I only want to call my JMS Adapter once, and in return I want maximum of 100 messages to be returned in response. Is it even possible ? I am using 12C of Fusion middleware. Please any points will be very helpful.
Bharat
  • 750
  • 1
  • 9
  • 20
1
vote
0 answers

Unable to get activity facebook share button (SDK 4.0)

I have a ListView inside each item I have a facebook share button
hinata
  • 23
  • 6
1
vote
2 answers

How to set the first position of a spinner to null

I have a Spinner I'm filling with a ArrayAdapter objects, but i need that first position of this Array Always be null or appear something like "Select an object". I searched here Forums but without success, solutions for ArrayList String or…
1
vote
0 answers

CURLOPT_FOLLOWLOCATION equivalent for socket adapter in Zend_Http_client

I would like to use the socket adapter instead of Curl for my code but however this the problem i am facing. (case 1 :) I am querying a server for response using Zend_http_client (default socket adapter) and I am getting a 404 error, (case 2 :)…
1
vote
1 answer

Can't save multiple custom objects in SharedPreferences?

Here is my issue. I can save an object, but if I save another object, it will erase the previous item. I'm using gson lib to save my items. After some researches I've seen this How to use SharedPreferences to save more than one values? But I can't…
Jay
  • 144
  • 1
  • 2
  • 14
1
vote
2 answers

ArrayIndexOutOfBoundsException when manipulating ArrayAdapter

Long story short: I have a listView with a customAdapter (called MathAdapter). This Adapter handles an ArrayList of type CalculationModel. By default there are ~15 CalculationModels in my List, however, under special conditions, i have to delete…
glace
  • 2,162
  • 1
  • 17
  • 23
1
vote
2 answers

Android RecyclerView move item to end of View

I am displaying Items on a Android RecyclerView, and I would like to be able to select any item, and place it at then very end of the list. I know how to Add or Remove items, but I am looking for the cleanest way to achieve this. If possible, I am…
Scaraux
  • 3,841
  • 4
  • 40
  • 80
1
vote
1 answer

How can I use getter and setter using class library in Android

I have show a list of records using RecyclerView. List records are 1 to 100 numbers. Now I want to show Names list from contacts class. So I want to create a Contact class where a variable Name (String) and has getter and setter methods. Now how can…
Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65
1
vote
0 answers

Android Listview ArrayAdapter Fade In/Out when element added/removed

Using an ArrayAdapter and an Android Listview, I would like to be able to automatically start an animation with each row of the Listview, when the element is added or removed from my ArrayList. I found a lot of tutorials, but they work with a…
Scaraux
  • 3,841
  • 4
  • 40
  • 80
1
vote
2 answers

How to get position of added in adapter object

I'm making a bluetooth connection. First I list the name and address of they in a ListView: pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); pairedListView.setOnItemClickListener(mDeviceClickListener); I create aמ…
1
vote
2 answers

Android Beginner: Adapter for simple view or direct DB query?

First off I'm really new to android (< 4 days). I'm trying to wrap my head around how database data is linked to Views and widgets. I've done a few tutorials and I've noticed that Adapters are used to link AdapterViews which are (as I understand…
nebs
  • 4,939
  • 9
  • 41
  • 70
1
vote
0 answers

NULL in Adapter in Fragment (FragmentPagerAdapter) after screen rotation

I was looking answer in similar topics but nothing works for me. I have Activity which has a fragment. This fragment has adapter. Everything is fine even if I rotate screen, but when I rotate screen and try to update adapter (for example by sort…
1
vote
1 answer

android filter listview works only for the first search

i am been trying to figure what seems to be the issue, but can get my head round it, basically i am filtering my listview which uses custom adapter that has images and text. its works first time e.g. when the activity runs for the first time it has…
Emotional_Goose
  • 167
  • 1
  • 13
1 2 3
99
100