25

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?

asked Jun 17, 2011 at 8:40
3
  • 3
    Then why don't you just add second list's items to the first list? Use listOne.addAll(listTwo) Commented Jun 17, 2011 at 8:44
  • Why did you roll back the question twice? It ended up with Sathya's revision anyway. Commented Jun 17, 2011 at 15:40
  • i am sorry it just happened like an accident.... Commented Jun 17, 2011 at 17:08

3 Answers 3

56

Combine two ArrayList into a single one

firstname1.addAll(first);

Please refer this article for sample code to concat two lists.

How to show these items in list view :

your layout should be ( as I used main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:background="@android:color/transparent">
 <ListView
 android:id="@+id/custom_list_view"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:cacheColorHint="#00000000"
 android:fastScrollEnabled="true"
 android:background="@android:color/transparent"
 android:fadeScrollbars="true"
 android:layout_gravity="top"
 android:padding="2dp">
 </ListView>
</LinearLayout> 

and now Activity as CustomListView.java

public class CustomListView extends Activity {
 ArrayList<String> firstname1;
 ArrayList<String> first;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 firstname1 = new ArrayList<String>();
 first = new ArrayList<String>();
 //Let both array list having some data
 firstname1.add("firstname1_data1");
 firstname1.add("firstname1_data2");
 firstname1.add("firstname1_data3");
 firstname1.add("firstname1_data4");
 firstname1.add("firstname1_data5");
 firstname1.add("firstname1_data6");
 firstname1.add("firstname1_data7");
 firstname1.add("firstname1_data8");
 firstname1.add("firstname1_data9");
 firstname1.add("firstname1_data10");
 first.add("first_data1");
 first.add("first_data2");
 first.add("first_data3");
 first.add("first_data4");
 first.add("first_data5");
 first.add("first_data6");
 first.add("first_data7");
 first.add("first_data8");
 first.add("first_data9");
 first.add("first_data10");
 //Now copying value of first to firstname, as your requirement
 //Please refer http://www.java-examples.com/append-all-elements-other-collection-java-arraylist-example for sample code to concat two lists.
 firstname1.addAll(first);
 //Lets show your data into list view
 // Get a handle to the list view
 ListView lv = (ListView) findViewById(R.id.custom_list_view);
 lv.setAdapter(new ArrayAdapter<String>(CustomListView.this,
 android.R.layout.simple_list_item_1, firstname1));
 //Please refer http://developer.android.com/reference/android/widget/ListView.html for details of setAdapter()
 }
}

Happy coding.

answered Jun 17, 2011 at 10:04
2
  • 1
    What about two different data type? Commented Jul 18, 2017 at 4:48
  • 2
    @Mr.Hyde ArrayList can not contain data with different data type. So you can not do it. Commented Jul 18, 2017 at 5:45
11
List<String> a = new ArrayList<String>();
a.add("bla");
a.add("bla");
a.add("bla");
List<String> b = new ArrayList<String>();
b.add("Boo");
b.add("Boo");
b.add("Boo");
// Append content of a to b
b.addAll(a);
// New list containing a union b
List<String> union = new ArrayList<String>(a);
union.addAll(b);

To show that in a list view, you will need an adapter along with a list view. I recommend you read the Android Developer guide's tutorial about ListView: Hello ListView

public class HelloListView extends ListActivity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 List<String> b = new ArrayList<String>();
 b.add("Boo");
 b.add("Boo");
 b.add("Boo");
 setListAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1, b));
 }
}
answered Jun 17, 2011 at 8:51
0

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
}
answered Apr 30, 2020 at 10:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.