Passing an ArrayList from string arrays from one activity to another in android

I want to pass an ArrayList from string arrays from one activity to another in Android.
How to use intent or bundle ? Please note that intent.putStringArrayListExtra does not work in this case, because it is not for string arrays.

+4
source share
4 answers

Not sure what you mean by "ArrayList String Arrays"

If you have an array of strings check the link below

Passing an array of strings between android actions

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

ArrayList implements Serializable

You can use intentions

  ArrayList<String> mylist = new ArrayList<String>(); Intent intent = new Intent(ActivityName.this, Second.class); intent.putStringArrayListExtra("key", mylist); startActivity(intent); 

Receive

  Intent i = getIntent(); ArrayList<String> list = i.getStringArrayListExtra("key"); 

public Intent putStringArrayListExtra (String name, ArrayList<String> value)

Add advanced data to the intent. The name should include a prefix package, for example, the application com.android.contacts will use names like "com.android.contacts.ShowAll".

Options

 name The name of the extra data, with package prefix. value The ArrayList data value. 

Returns

 Returns the same Intent object, for chaining multiple calls into a single statement. 

Pass Array ArrayList Array

 String[] people = { "Mike Strong", "Jennifer Anniston", "Tom Bennet", "Leander Paes", "Liam Nesson", "George Clooney", "Barack Obama", "Steve Jobs", "Larry Page", "Sergey Brin", "Steve Wozniak" }; String[] people1 = { "raghu", "hello" }; ArrayList<String[]> list = new ArrayList<String[]>(); list.add(people); list.add(people1); Intent i = new Intent(MainActivity.this,SecondActivity.class); i.putExtra("key", list); startActivity(i); 

Receive

 Intent in = getIntent(); ArrayList<String[]> list =(ArrayList<String[]>) in.getSerializableExtra("key"); for(int i=0;i<list.size();i++) { String s[]= list.get(i); for(int iv=0;iv<s.length;iv++) Log.i("..............:",""+s[iv]); } 
+13
source

I am not familiar with this if it is doable or not, as things stand with the Bundle class, so I would use a custom object to place your ArrayList. this is a good clean solution for posting other common data that you will need to get in both steps.

 public class MyCustomData implements Serializable { static final long serialVersionUID = 42432432L; public ArrayList<String[]> strings = new ArrayList<String[]>(); public MyCustomData() { }; } 

and then in your activity:

 MyCustomData myCustomDataInstance = new MyCustomData(); myCustomDataInstance.strings = //set it here; Bundle bundle = new Bundle(); Intent selectedIntent = new Intent(getActivity(), MyNextClass.class); bundle.putSerializable("key", myCustomDataInstance); selectedIntent.putExtras(bundle); startActivity(selectedIntent); 

I would also suggest using arraylist arraylists instead of arraylist arrays

+1
source

So you want to pass a list of string arrays, not a list of strings, right? Since ArrayList is serializable, you can add it quite easily:

 ArrayList<String[]> list = new ArrayList<String[]>(); // Add some String[] Intent i = new Intent(); i.putExtra("xxx", list); // And to get it back ArrayList<String[]> list = (ArrayList<String[]>) i.getSerializableExtra("xxx"); 
0
source

This problem has a simple solution, you can simply declare ArrayList public and static in your first action, and then call it in another.

In your first action

 public static ArrayList<String> myList = new ArrayList(); myList.add("some_value"); 

In the second action

 Toast.makeText(getApplicationContext(),FirstActivity.myList.size().toString(),Toast.LENGTH_SHORT).show(); 
-1
source

Source: https://habr.com/ru/post/1489544/


All Articles