2

I have a list of object named

List<Products> data

Now I want to split this list by 10 items, so if I have 24 items in data list I want to create and fill 3 new List

sorry if this question is kinda confusing

EDIT

What I really what to do is to strip a long list of data into clusters, each cluster have 10 items. The reason I want to strip is I want to make a pagination instead of long list, each page have 10 items.

ListView lv;
List<Products> data

for (Products product : data) {

*I want to split the list here*

    }

CustomAdapter adapter = new CustomAdapter(getContext(), data);
lv.setAdapter(adapter);
  • 2
    Please show some effort first. – Rahman Jan 15 '16 at 07:18
  • 1
    Same question for Java 8, but at least the solution with Guava works also on Android: http://stackoverflow.com/questions/28210775/split-list-into-multiple-lists-with-fixed-number-of-elements-in-java-8?lq=1 – Thilo Jan 15 '16 at 07:22
  • 1
    Add more specifications here. Becuase Solution can be achieved in many good and vague ways.Vague one like create two list and then add first 10 and then rest 10 and then the left over.I am sure you are looking for something different.Please elaborate your question more – Manish Singh Jan 15 '16 at 07:24
  • I edited my question. sorry for my unclear question, I kinda new here :) – Cora Contrabida Jan 15 '16 at 07:56

1 Answers1

1

You can use below kind of method for splitting

public static List<List<String>> splitData(List<String> data) {
        List<List<String>> splittedData = new ArrayList<List<String>>();
        List<String> currentSplit = null;
        for (int i = 0; i < data.size(); i++) {
            if (i % 10 == 0) {
                currentSplit = new ArrayList<String>();
                splittedData.add(currentSplit);
            }
            currentSplit.add(data.get(i));
        }
        return splittedData;
    }

Instead of <String> you need to use <Products>

Dev Blanked
  • 8,555
  • 3
  • 26
  • 32