[英]Passing Parcelable Object between Intents
I am having an issue passing an object I have created in between events. 我在传递事件之间创建的对象时遇到问题。 I used the website http://www.parcelabler.com/ to create the parcelable element of the code. 我使用http://www.parcelabler.com/网站创建了代码的parcelable元素。 The object class is show below: (The Item class is another simple object containing Strings and doubles and has also been made parcelable) 对象类如下所示:(Item类是另一个包含Strings和doubles的简单对象,也已使其可拆分)
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public class Diner implements Parcelable {
private String name;
private ArrayList<Item> itemList = new ArrayList<Item>();
public Diner(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void addItem(Item foodItem) {
itemList.add(foodItem);
foodItem.incrementBuyerCount();
}
public double getPrice() {
double total = 0;
for(Item item : itemList) {
total += item.getPrice() / item.getBuyerCount();
}
return total;
}
protected Diner(Parcel in) {
name = in.readString();
if (in.readByte() == 0x01) {
itemList = new ArrayList<Item>();
in.readList(itemList, Item.class.getClassLoader());
} else {
itemList = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
if (itemList == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(itemList);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Diner> CREATOR = new Parcelable.Creator<Diner>() {
@Override
public Diner createFromParcel(Parcel in) {
return new Diner(in);
}
@Override
public Diner[] newArray(int size) {
return new Diner[size];
}
};
}
In my main activity, I have a button which opens an 'Add Diner' activity, when a button is pressed and waits for a result. 在我的主要活动中,我有一个按钮,当按下按钮并等待结果时,该按钮将打开“添加晚餐”活动。
private final int SET_REQUEST = 1;
addDinerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AddDinerActivity.class);
startActivityForResult(intent, SET_REQUEST);
}
});
The Add Diner activity is opened, the user enters a String in a Diner Name EditText which is used the create a new Diner object and returns to the main activity when an OK button is pressed. 将打开“添加用餐者”活动,用户在用餐者名称EditText中输入一个字符串,该字符串将用于创建新的用餐者对象,并在按下“确定”按钮时返回到主活动。
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = getIntent();
Diner newDiner = new Diner(dinerNameEditText.getText().toString());
intent.putExtra("newDiner", newDiner);
setResult(RESULT_OK, intent);
finish();
}
});
Finally the Diner object is received and added to an array in the main activity: 最后,接收Diner对象并将其添加到main活动的数组中:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode == SET_REQUEST) {
Diner newDiner = getIntent().getParcelableExtra("newDiner");
dinerList.add(newDiner);
}
}
}
Unfortunately my code is crashing when I try to save the Diner object and pass it to the main activity, can anyone see why this is? 不幸的是,当我尝试保存Diner对象并将其传递给main活动时,我的代码崩溃了,有人可以看到为什么吗?
使用onActivityResult
方法的data
第三个参数而不是getIntent()
来从Intent获取数据,该数据是从使用startActivityForResult
启动的Activity发送的:
Diner newDiner = data.getParcelableExtra("newDiner");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.