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
14
votes
6 answers

Implementing Circular Scrolling In PagerAdapter

I am using PagerAdapter for horizontal swiping for showing newspaper pages in my app. Currently I want to implement the circular scrolling in this app.Right now what I have done is whenever I am getting on last page I try to set the currentItem to…
Shashank_Itmaster
  • 2,465
  • 5
  • 30
  • 56
14
votes
1 answer

Are most PHP frameworks actually MVA instead of MVC?

Many PHP frameworks claim that they implement MVC design pattern. However, in their implementation, the model and view don't know each other and each communication in between must be done through controller. As I read in wikipedia, this is MVA…
LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
14
votes
4 answers

Android ListView Adapter how to detect an empty list?

I have a ListView in my Activity which takes values from an extension class of SimpleAdapter. This one is correctly working when I add or delete some data in it. But my problem is, when my item list is empty, I would like to replace the listView by…
Weber Antoine
  • 191
  • 2
  • 2
  • 10
14
votes
3 answers

Animate newly added items in ListView

How can I animate newly added items in ListView? I have a adapter and when I add new items in my list I say adapter.notifyDataSetChanged(); the items are added, everything works perfectly, but my problem is I want newly added element to have some…
Lukap
  • 31,523
  • 64
  • 157
  • 244
14
votes
1 answer

android - disable Listview item click and re-enable it

So I have the following code in the adapter: @Override public boolean isEnabled(int position) { GeneralItem item = super.getItem(position); boolean retVal = true; if (item != null) { …
dropsOfJupiter
  • 6,763
  • 12
  • 47
  • 59
14
votes
2 answers

Moshi: Parse single object or list of objects (kotlin)

Problem How to parse either a single Warning object or a list of Warning objects (List) from an API using Moshi? The response as a single warning: { "warnings": {...} } The response as a list of warnings: { "warnings": [{...},…
Niclas
  • 510
  • 6
  • 19
14
votes
3 answers

The correct way of implementing getItemId() in RecyclerView.Adapter

I have generic class public abstract class BaseAdapter extends RecyclerView.Adapter { private List itemsList = new ArrayList<>(); //other override methods @Override public long getItemId(int position) { return position; …
Sky
  • 521
  • 1
  • 5
  • 14
14
votes
1 answer

When getItemCount and getItemViewType method get called in Recycler Adapter

I saw similar questions in stackoverflow but they doesn't give clear answers to my question. Don't mark it as duplicate before reading my full questions. I saw this link , this , and this too. Thanks for spending your time to read this. I gave my…
Ganesh
  • 1,820
  • 2
  • 20
  • 40
14
votes
4 answers

how to add item to Spinner's ArrayAdapter?

i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code: public class Spr…
allen
  • 141
  • 1
  • 1
  • 4
14
votes
10 answers

"Adapter" or "adaptor"?

In programming, which spelling—adapter or adaptor—is standard or de facto standard? Is there a difference between them? In boost I see "adaptor", whereas in literature I see "adapter". Which one is preferred in code?
Anycorn
  • 50,217
  • 42
  • 167
  • 261
14
votes
3 answers

android listviews: header and footer views

In my ListActivity, I need header and footer views (on the top and bottom of the list) to be used as previous page and next page buttons on my list, respectively, because I want to display only 20 items at a time. I set my header and foot views by…
jlim
  • 801
  • 2
  • 13
  • 21
14
votes
1 answer

Page change callback for PagerAdapter

I am trying to develop an app which will display numbers 1-10 and along with it a sound for that number. Using the example in mobile.tutsplus for horizontal swiping, I have reached to a stage where all the numbers are displayed correctly but sound…
Nikhil
  • 143
  • 1
  • 1
  • 5
14
votes
3 answers

how to know from adapter if the item of ListView is visible or not?

I have an adapter that connected to ListView, e.g. mJournalAdapter = new JournalAdapter(); journalEntryList.setAdapter(mJournalAdapter); and I want to know inside of my JournalAdapter if some view (item of ListView) is visible or not, is it…
olegflo
  • 1,105
  • 3
  • 17
  • 26
14
votes
3 answers

How to get selected list items from a Listview with checkBox and Custom Adapter?

I have a ListView with CheckBox on it. and i am using Custom Adapter to populate the ListView. In my xml file i have a Button at bottom. what i want is let user select number of rows in ListView and when he/she clicked on the Button get the position…
Akram
  • 7,548
  • 8
  • 45
  • 72
13
votes
3 answers

Deserializing ImmutableList using Gson

I'm using quite a few immutable collections and I'm curious how to deserialize them using Gson. As nobody answered and I've found the solution myself, I'm simplifying the question and presenting my own answer. I had two problems: How to write a…
maaartinus
  • 44,714
  • 32
  • 161
  • 320