简体   繁体   中英

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. 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];
        }
    };
}

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.

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);
        }
    }
}

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方法的data第三个参数而不是getIntent()来从Intent获取数据,该数据是从使用startActivityForResult启动的Activity发送的:

Diner newDiner = data.getParcelableExtra("newDiner");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM