Reputation:
Need help in getting values from arraylist then save to another arraylist (which is in another class file)
Upvotes: 0
Views: 406
Reputation: 42016
use Intent
for passing value to another class(extends activity) intentObj.putExtras()
will help.
to put:
ArrayList<String> arrayList= new ArrayList<String>();
arrayList.add("hello");
arrayList.add("there");
Intent intent = new Intent(getApplicationContext(), secondClass.class);
intent.putStringArrayListExtra("pass_list", arrayList);
startActivity(intent);
to get
ArrayList<String> arrayList= getIntent().getStringArrayListExtra("pass_list");
Upvotes: 3
Reputation: 9295
You can use Collection class's addAll method to add all elements of an arraylist to another.
See the documentation http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#addAll%28java.util.Collection%29
Upvotes: 0
Reputation: 68857
Use get(index)
, set(index, object)
, insert(index, object)
and add(object)
methods for this.
Example:
List<String> list0 = new ArrayList<String>();
list0.add("this");
list0.add("is");
list0.add("an");
list0.add("answer");
List<String> list1 = new ArrayList<String>();
list1.add(list0.get(1));
list1.add(list0.get(3));
list1.insert(1, list0.get(0));
The resulting list1 will be: "is", "this", "answer".
Upvotes: 0