user1332993
user1332993

Reputation:

Getting values from arraylists and saving

Need help in getting values from arraylist then save to another arraylist (which is in another class file)

Upvotes: 0

Views: 406

Answers (3)

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

krishnakumarp
krishnakumarp

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

Martijn Courteaux
Martijn Courteaux

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

Related Questions