Reputation: 3219
I need a little assistance here!
I have followed this guide for getting some results.
And adapter is set up, recyclerview is initialized, but i'm having trouble with showing right data in list.
So here is what i'm trying to do:
First i'm storing in temporarily list data fetched from api and also creating one array list of dates. Of course i'm removing duplicate dates from that array list because there are some items ordered by same date.
And here is some code and how i'm populating recyclerview and ordering items by dates in sections:
// dates - array list of dates(16 February, 20 February..)
// mEvents - temporarily list with size of 8 items
// originalList - list of events ordered by date
for (String date : dates) {
originalList.clear();
for (Event event2 : mEvents) {
// Checking if item's date from temp. list is equal with
// iterated date and adding to original list
if (date.equals(event2.getDate())) {
originalList.add(event2);
}
}
// Attaching section to adapter with iterated date and array list related to that date
EventSection eventSection
= new EventSection(R.layout.lst_item_event_header, R.layout.lst_item_event_v2, date, originalList, getActivity());
mSectionedRecyclerViewAdapter.addSection(eventSection);
}
And problem here is that i'm getting for every date section last 2 items from original list. What i'm missing here?
EDIT:
// I need to sort items by these dates into sections
Dates:: [2017-02-16, 2017-02-17, 2017-02-28, 2017-02-22, 2017-02-20]
Event date:: 2017-02-16
Event date:: 2017-02-16
Event date:: 2017-02-17
Event date:: 2017-02-17
Event date:: 2017-02-28
Event date:: 2017-02-22
Event date:: 2017-02-20
Event date:: 2017-02-20
Upvotes: 1
Views: 3284
Reputation: 3219
Okay, i have finally found a solution with the help of examples of autor's library. So if anyone else was having trouble like me, this will maybe help you:
for (String date : dates) {
originalList = getEventsWithDate(date);
if (originalList.size() > 0) {
mSectionedRecyclerViewAdapter.addSection(new EventSection(date, originalList, getActivity()));
}
}
private List<Event> getEventsWithDate(String date) {
List<Event> events = new ArrayList<>();
for (Event event : mEvents) {
if (date.equals(event.getDate())) {
events.add(event);
}
}
return events;
}
Upvotes: 2
Reputation: 2372
You do have a nested for loop, so my guess is that the problem is somewhere in there. Double check the condition in there if that is what you want.
for (String date : dates) {
// Clear the list here so you render a new list of dates that equal the
// event date with the date of this iteration.
originalList.clear();
for (Event event2 : mEvents) {
if (date.equals(event2.getDate())) { // Check this condition
originalList.add(event2);
}
}
...
}
If that would be true for every time you will end up with 64 items. I don't know what your data it, so I can't verify this for you.
What probably happening is that, by not clearing the list, you are adding a bigger and bigger list to the view each time. Clear the list before adding new ones to it and you should only render the list you want.
Upvotes: 1