Answer the question
In order to leave comments, you need to log in
Android RecyclerView. What is the correct way to get data for a list item from multiple sources?
I had a collection of certain objects which I fed to the adapter. The adapter placed the necessary data that was stored in this object in the right places.
Now I'm extending the app's capabilities. And I need to display in the RecyclerView element the data that is stored not only in one object of a certain class, the collection of which can be passed to the adapter, but also in another new object of the new class.
Let me explain with an example:
Previously , the "name" and "surname" fields from the user object of the User class were displayed in the recycler element.
Now , in the same recycler element, I need to put the "orderName" field from the order object of the Order class next to it
(User and Order are also tables in the database. I use the Google ROOM library)
The problem is that the adapter only accepts one collection of defined objects.
How to properly implement this task? Maybe this task looks like some kind of pattern or there is something that programmers use everywhere (since the task is very typical), but I don’t know
Answer the question
In order to leave comments, you need to log in
For the adapter, make a new model (not the one taken from the database), which will aggregate all the necessary data in itself. Map physical models to gouish ones in a presenter or interactor.
As I understand it, there is a connection between User and Order, right?
In this case, you can pass to the adapter not a collection of users, but a collection of UserOrder objects (well, or come up with a better name ;-) ), which will contain all the necessary information.
Read about composition and aggregation
What prevents the Order field from adding the User field and already passing the orders collection to the adapter?
you can aggregate your current models within a common model, you can make a common successor (or Object) for them
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question