How to combine two array list and show in a listview in android

Viewed 67824

I want to combine two ArrayList into a single one.

My first arraylist looks like this:

{a,s,d,f,g,h,......}

My second arraylist looks like this:

{z,x,c,v,b,.....}

Then I want to combine both to be as

  {a,s,d,f,g,h,.....,z,x,c,v,b.....}

First List is

ArrayList<String> firstname1   = new ArrayList<String>();

Where as the second list is as

ArrayList<String> first   = new ArrayList<String>();

Now I want to combine all this together and I want it to be listed out in list view. How to do this?

3 Answers

if both the List (like List<String> and List<Integer>) have different data types

List<String> stringsList= new ArrayList<>();
stringsList.add("A string");
stringsList.add("another string");
stringsList.add("and one more");

List<Integer> integersList = new ArrayList<>();
integersList.add(1337);
integersList.add(1338);
integersList.add(1339);

// New list containing all the items form
// stringsList and integersList
List<Object> allIWeHave= new ArrayList<>();
allIWeHave.addAll(stringsList);
allIWeHave.addAll(integersList);

//while fetching you check either item is Integer or String
if(allIWeHave.get(0) instanceof Integer){
//Integer value
}else{
//String value
}
Related