Closed Minification closed 4 years ago
Hi, Regarding your questions:
It is not working probably because a line after the clear you are adding items to the adapter, thus clearing the empty module and inserting an item module. why are you calling clear on each observation callback? when using observation with a full item list, it is best to use setItems() and let the adapter calculate the difference between the old and the new list automatically. if the list is empty, the empty module will be invoked, if not the adapter will render the changes. no need to call clear, or add in this case.
The itemcount variable is used by the internals of the adapter, it may hold more items than you think to support various functions. Adding a function the will return the number of items per type (let's say a T model) will be helpful?
First, try what I wrote in question 1 and see if the issue is resolved. Else, From your code, the id looks fine but there is a setId() function that I don't know if you calling from outside. The exception means 2 models have the same ID, are you 100% that's not the case?
Hi, thanks for your reply.
I only added the call to clear() because for some reason I can see more items displayed than I added as can be seen in the screenshot I provided (there are 5 items visible but only 3 should be there (because of the call to clear)).
I see. I only added the output line to check how many items are in the adapter because the logics didn't add up with the visuals.
I removed the call to clear and printed the ids of the items to be added to the console. Every id in the output is unique, which is why I don't understand why the exception would occur. With the clear() removed, the output is as follows, where each output block is what is printed in the observation callback:
CreateMarkerDialogKotlin$onActivityCreated: item id is 1
...
CreateMarkerDialogKotlin$onActivityCreated: item id is 1
CreateMarkerDialogKotlin$onActivityCreated: item id is 2
...
CreateMarkerDialogKotlin$onActivityCreated: item id is 1
CreateMarkerDialogKotlin$onActivityCreated: item id is 2
CreateMarkerDialogKotlin$onActivityCreated: item id is 3
And when you get these logs and crash, are you using add() or setItems()?
Because add() will result in the behavior you are describing and it's intended by design. The items will be added to the previous items, resulting in more and more items, plus the ids are the same since id=1 for example, is inserted 3 times. setItems() will compute a diff between the current list and the new list and send updates only when needed.
I see now, that is why those problems arise. I didn't know add() behaved like this by design (I thought it handled the diff calculation and setItems was just a setter).
With that knowledge, I'm closing the issue of course, though unless I missed it, I feel the documentation should include this rather important detail.
Come to think of it, I don't even know why I used add() instead of setItems()...
Thanks for your help, this definitely restores my faith in this library.
Hi,
I appear to have the same problem again that I have had numerous times before, plus an additional but maybe related problem. Skip to the bottom to see a summary.
Exception:
What did I do?
I added 4 items to a
RecyclerView
backed with aOneAdapter
.How do I setup the
RecyclerView
andOneAdapter
in question?In the
Fragment
:PhotoItemModule:
VoiceOverEmptinessModule:
The item:
How do I add items to the adapter?
In the
Fragment
:Here,
createMarkerDialogViewModel
is aViewModel
andphotoListLiveData
is aLiveData
.What do I see on the screen before the app crashes?
The following screenshot was taken after the third item was inserted. NOTE that each pair of the picture and the "a>z"-Image consitutes one item, but we count at least five pairs, i.e. at least 5 items:
My questions
photosOneAdapter.clear()
call not seem to work?If anything is unclear or I forgot something, notify me and I'll provide the necessary information. Since this is part of a larger project at uni I can't really show the whole source, but I'll do my best to help.