Android, How to place an ArrayList <myObject> in an intent?

I have two operations, in the first, I initialize the ArrayList of myObject. In the second exercise, I need to get this Arraylist. I do not know how to do this with an intention?

(An object is a class that I created)

Thanks in advance.

+6
source share
4 answers

If you make your Object Parcelabel class, you can pack your arraylist in the package that you send with the intent

see this link for an example

+6
source

You typically use Bundle objects to pass information between actions, but they only allow simple type objects. As a rule, to transfer more complex types of objects, you usually have to create some kind of static context and set your own values ​​for it, which are then available for the second action. It feels dirty, but now I have it in my applications.

+1
source

Your myObject class will need to implement Parcelable . Then you can use putParcelableArrayListExtra from your intent to pass it to the next action and get the list using getParcelableArrayListExtra

+1
source

I used this.

set:

 intent.putExtra("data", new DataWrapper(selectedTasks)); startActivity(intent); 

we get:

 DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("data"); ArrayList<SelectedTask> taskList = dw.getList(); 

Arraylist object must be Serializable

 public class SelectedTask implements Serializable{ } public class DataWrapper implements Serializable{ private ArrayList<SelectedTask> slist; public DataWrapper(ArrayList<SelectedTask> data) { this.slist = data; } public ArrayList<SelectedTask> getList() { return this.slist; } } 
0
source

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


All Articles