0

I would like to have a few similar expandable listviews in my android application across a few activities. They all have the same content, but use different designs. Some have buttons for example, and some not.

I would even want to have three expandable listviews that share everything except the background color for each cell.

Anyways, having a unique adapter for each listview seems like a bad design practice as they all very similar. I was thinking of making an abstract adapter and than extending it for every listview, would that work? Is there any other approach?

Thank you.

Berry Jones
  • 857
  • 2
  • 7
  • 11
  • "I would even want to have three expandable listviews that share everything except the background color for each cell." - Sonds like only your layout resource is different. Use one adapter for all 3 listviews and pass your layout resource in the constructor. – zyamys May 08 '16 at 01:59

1 Answers1

1

Having a unique adapter for each listview seems like a bad design practice as they are all very similar.

That is correct. Duplicated code is almost always a sign of bad code design.

I was thinking of making an abstract adapter and then extending it for every listview, would that work?

Yes.

Is there any other approach?

Yes. E.g. if you want to have an adapter that supports different backgrounds, your adapter should take a background resource/color as constructor parameter. This approach fits for minor changes. If you want to have some non-expandable and some expandable listviews I would favor the inheritance approach.


The problem is that some of my listviews would be expandable, so I could not use Recycler View.

Recyclerviews are a better design choice than listviews. Creating expandable ones isn't too hard, see Expandable list with RecyclerView?.

Community
  • 1
  • 1
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • Thank you! Since it's my first app and I don't have to deal with a ton of data, I would stick with basics (listView), but I would certainly keep in mind that RecyclerView is generally the better design choice – Berry Jones May 08 '16 at 02:46