Best way to filter List every 'x' items

223 Views Asked by At

I have a listView and I'm trying to display every 10 seconds like a new 'page' of data in this listview.

To do that, I use a kind of filter on my list of data every 5 items:

List<List<CustomObject>> pages = new ArrayList<List<CustomObject>>();

for (int i = 0; i < listToFilter.size(); i += 5) {

    if ((i + 5) <= listToFilter.size()) {
        // create a group of 5 custom objects
        List<CustomObject> group = listToFilter.subList(i, i + 5);
        pages.add(new ArrayList<CustomObject>(group));

    } else {
        // add the remaindering custom objects in an other list
        int remainder = listToFilter.size() - i;
        List<CustomObject> group = listToFilter.subList(i, i + remainder);
        pages.add(new ArrayList<CustomObject>(group));
    }
}

At the end of this code, I have my list 'pages' which contains different lists with a maximum size of 5, for instance:

pages = {java.util.ArrayList@831696467904} size = 2 // page list
[0] = {java.util.ArrayList@831698108576} size = 5 // page1
[1] = {java.util.ArrayList@831698106184} size = 2 // page2

So my goal here, is to iterate on these pages to display each 10 seconds a new page. Like a book using listView and an adapter to browse its pages.

I have a Timer which call my adapter every 10 seconds and swap my data to update the interface:

... // My filter job
this.notifyDataSetChanged();
clear();
addAll(pageData);

At the moment everything works but each time I have to recreate a new list to do this job.

I would like to know if there is a solution to just use a filter (I'm not using java8) on my "listToFilter" instead of going through this complex custom filter which recreate a new list every time.

Edit:

I found a way to manage this by using Range with guava. You can define a range of item to browse in your list. But at the moment I'm not sure yet how to use it and iterate on your range list with a custom object.

0

There are 0 best solutions below