Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...