Need help in getting values from arraylist then save to another arraylist (which is in another class file)
2
-
3What did you try? Where are you failing? What are you expecting? What are you getting?npinti– npinti2012-04-14 07:28:57 +00:00Commented Apr 14, 2012 at 7:28
-
1"Need help" Need question. As well as the answers to the 4 questions asked by @npinti Voting to close as 'not a real question' since SO is a Q&A site, not a 'tutor me in detail' site.Andrew Thompson– Andrew Thompson2012-04-14 07:40:30 +00:00Commented Apr 14, 2012 at 7:40
Add a comment
|
3 Answers
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");
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".
Comments
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