Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass list of objects from one activity to other activity in android

I want to pass a list of objects from one activity from another activity. I have one class SharedBooking Below:

public class SharedBooking {   public int account_id;   public Double betrag;   public Double betrag_effected;   public int taxType;   public int tax;   public String postingText; } 

Code from Calling activity:

public List<SharedBooking> SharedBookingList = new ArrayList<SharedBooking>();  public void goDivision(Context context, Double betrag, List<SharedBooking> bookingList) {   final Intent intent = new Intent(context, Division.class);       intent.putExtra(Constants.BETRAG, betrag);           intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     context.startActivity(intent);         } 

COde on called activity:

Bundle extras = getIntent().getExtras(); if (extras != null) {   amount = extras.getDouble(Constants.BETRAG,0); } 

How can I send the list of SharedBooking from one activity and receive that on other activity?

Please suggest me any usable link or sample code.

like image 757
Sushant Bhatnagar Avatar asked Aug 23 '12 13:08

Sushant Bhatnagar


People also ask

How do I move a list from one activity to another activity?

You can pass an ArrayList<E> the same way, if the E type is Serializable . You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval. Example: ArrayList<String> myList = new ArrayList<String>(); intent.

How do I pass model data from one activity to another in Android?

We can send the data using putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you pass a list on Kotlin?

Typically it is complicated to pass a list of objects between Activities. But lately in Kotlin all you have to do is simple. First, in your data class you implement Serializable. Then in your source Activity, you add it to your bundle and cast it as Serializable.


1 Answers

First, make the class of the list implement Serializable.

public class MyObject implements Serializable{} 

Then you can just cast the list to (Serializable). Like so:

List<MyObject> list = new ArrayList<>(); myIntent.putExtra("LIST", (Serializable) list); 

And to retrieve the list you do:

Intent i = getIntent(); list = (List<MyObject>) i.getSerializableExtra("LIST"); 

That's it.

like image 104
Ruzin Avatar answered Oct 11 '22 03:10

Ruzin