传球意图之间Parcelable对象 [英] Passing Parcelable Object between Intents
问题描述
我有一个问题,我路过了事件之间创造了一个对象。我用的网站 http://www.parcelabler.com/ 创建$ C $的parcelable元素C。 Object类下面显示:(Item类是包含字符串和双打另一个简单的对象,并也取得了parcelable)
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. The object class is show below: (The Item class is another simple object containing Strings and doubles and has also been made parcelable)
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];
}
};
}
在我的主要活动中,我有一个打开添加晚餐的活动,当按钮被pressed并等待结果。
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);
}
});
在添加晚餐活动开启后,用户将在一个晚餐名称的EditText一个字符串,它使用了创建一个新的晚餐对象,并返回到主活动时确定按钮是pressed。
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.
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:
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);
}
}
}
不幸的是,当我尝试保存晚餐对象,并把它传递给主要活动,任何人都可以看到,为什么这是我的code崩溃?
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?
推荐答案
使用数据
的第三个参数的onActivityResult
方法而不是 getIntent()
用于获取意图,它是由使用 startActivityForResult
开始活动发送的数据:
Use data
third parameter of onActivityResult
method instead of getIntent()
for getting data from Intent which is sent from Activity which is started using startActivityForResult
:
Diner newDiner = data.getParcelableExtra("newDiner");
这篇关于传球意图之间Parcelable对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!