我正在尝试将数组列表传递回我的父 Activity
这是简单的代码。
private ArrayList<Receipt> receipts = new ArrayList<Receipt>();
Intent data = new Intent();
data. // what to do here?
setResult(RESULT_OK, data);
//************************************
这是基本的收据类
public class Receipt {
public String referenceNo;
public byte[] image;
public String comments;
public Date createdOn;
public Date updatedOn;
告诉我如何将它添加到我的 Intent 中以及如何在父 Activity 中将其取回
onActivityResult(final int requestCode, int resultCode, final Intent data)
最佳答案
您可以使用 data.putExtra()
/bundle.putSerializable()
将自定义对象放入 Bundle/Intent 中,但是,您必须让您的类 Receipt implement Serializable
(只需将 implements Serializable
放在类名之后,这就是一个简单类的全部内容。
然后使用 getSerializable()
读回它。
注意:我不认为 Date
类是可序列化的,您可能想用以毫秒为单位的较长时间来替换那些(您可以使用Date.getTime()
的)。
示例:
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");
关于java - 将数组列表发送回父 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9410740/