Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending an arraylist back to the parent activity

Tags:

java

android

i am trying to pass an arraylist back to my parent activity

Here is the simple code.

private ArrayList<Receipt> receipts = new ArrayList<Receipt>();


 Intent data = new Intent();
 data. // what to do here?
setResult(RESULT_OK, data); 

//************************************

This is basic receipt Class

public class Receipt {


    public String referenceNo;
    public byte[]   image;
    public String comments;
    public Date   createdOn;
    public Date   updatedOn;

Tell me how can i add it in my intent and how can i retrieve it back in parent activity from

onActivityResult(final int requestCode, int resultCode, final Intent data)
like image 382
Muhammad Umar Avatar asked Oct 24 '22 08:10

Muhammad Umar


1 Answers

You can use data.putExtra()/bundle.putSerializable() to put custom objects in a Bundle/Intent, however, you have to make your class Receipt implement Serializable (just put implements Serializable after the class name, that's all there is to it with such a simple class. Then to read it back you use getSerializable().

Note: I don't think that the Date-class is serializable, you might want to replace those with a long representing time in milliseconds instead ( you can use Date.getTime() for that).

Example:

public class Receipt implements Serializable{
    public String referenceNo;
    public byte[]   image;
    public String comments;
    public long   createdOn;
    public long   updatedOn;
    //...
}

//Put the list in the intent
List<Receipt> list = ...
Intent data = new Intent();
data.putExtra("tag", list);

//Read the list from the intent:
list = (List<Receipt>) data.getSerializableExtra("tag");
like image 81
Jave Avatar answered Nov 15 '22 01:11

Jave