I have been trying to look online for any solutions or examples on how to do that, but could not manage to find anything what resembles my problem.
I have a LinearLayout where I want to add/remove Views when ArrayList data changes.
As far as I understand, the only way is creating a CustomView by extending AdapterView and using ArrayAdapter.
Unfortunately I don’t understand the proper data-flow to solve this problem.
Where do I specify in CustomView which View is the container?
Can I just cast CustomView on LinearLayout when I will have implemented it?
EDIT:
I emphasize – I do not need a ListView. I need it for a CustomView
You do not need to extend AdapterView to generate your views from adapter in your custom view. You can extend
LinearLayoutand handle adapter. The simplest solution would look like:As the code above is only a simple example it does not handle situation where adapter returns views of different types (e.g.
Adapter#getViewTypeCount()returns number greater than 1).Of course all methods defined LinearLayout for adding/removing views are available so they may collide with your adapter handling. You could disable them by throwing UnsupportedOperationException:
(and so on for all other add/remove methods), or by overriding them to manipulate adapter’s backing dataset (which should be forced to implement your custom defined interface allowing such modifications next to Adapter interface). In both cases remember to call add remove methods from superclass in your code for adapter handling.
EDIT:
And simple implementation extending LinearLayout with support for Adapter’s viewTypesCount: