如何使用parcelable将对象从一个活动转移到另一个活动?

时间:2012-09-13 13:13:43

标签: android android-activity parcelable

我正在使用parcelable将对象从一个活动转移到另一个活动,但它显示为null 在另一个活动中检索该对象。 有什么建议吗?

以下是代码:

public class TPackage implements Parcelable {

// private static final long serialVersionUID = 1L;
private int cost;
private String name;
private int totalChannels;
private HashMap<String, Channel> channelList;

public int getCost() {
    return cost;
}

public void setCost(int cost) {
    this.cost = cost;
}

public int getTotal_channels() {
    return totalChannels;
}

public void setTotal_channels(int total_channels) {
    this.totalChannels = total_channels;
}

public HashMap<String, Channel> getList() {
    return channelList;
}

public void setList(HashMap<String, Channel> list) {
    this.channelList = list;
}

public void put(String name, Channel c) {
    channelList.put(name, c);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {

}

public static final Parcelable.Creator<TPackage> CREATOR = new Parcelable.Creator<TPackage>() {
    public TPackage createFromParcel(Parcel in) {
        return new TPackage();
    }

    public TPackage[] newArray(int size) {
        return new TPackage[size];
    }
};

}

2 个答案:

答案 0 :(得分:2)

这是使用parcel andorid传输对象的正确方法

public class TPackage implements Parcelable {

// private static final long serialVersionUID = 1L;
private int cost;
private String name;
private int totalChannels;
private HashMap<String, Channel> channelList;

public TPackage(int cost, String name, int totalChannels,
        HashMap<String, Channel> channelList){
    this.cost = cost;
    this.name = name;
    this.totalChannels = totalChannels;
    this.channelList = channelList;
}


public TPackage(Parcel in){
    //same order as that in writeToParcel()
    cost = in.readInt();
    name = in.readString();
    totalChannels = in.readInt();
    Bundle bundle = in.readBundle();
    channelList = (HashMap<String, Channel>) bundle.getSerializable("channelList");
}

public int getCost() {
    return cost;
}

public void setCost(int cost) {
    this.cost = cost;
}

public int getTotal_channels() {
    return totalChannels;
}

public void setTotal_channels(int total_channels) {
    this.totalChannels = total_channels;
}

public HashMap<String, Channel> getList() {
    return channelList;
}

public void setList(HashMap<String, Channel> list) {
    this.channelList = list;
}

public void put(String name, Channel c) {
    channelList.put(name, c);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(cost);
    dest.writeString(name);
    dest.writeInt(totalChannels);
    //easiest way to transfer HashMap via parcel
    Bundle bundle = new Bundle();
    bundle.putSerializable("channelList", channelList);
    dest.writeBundle(bundle);
}

public static final Parcelable.Creator<TPackage> CREATOR = new Parcelable.Creator<TPackage>() {
    public TPackage createFromParcel(Parcel in) {
        return new TPackage(in);
    }

    public TPackage[] newArray(int size) {
        return new TPackage[size];
    }
};

}

[EDITED] 这是发件人活动

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TPackage tPackage = new TPackage(10, "hello", 123, new HashMap<String, Channel>());
        Intent intent = new Intent(this, NActivity.class);
        intent.putExtra("parcel", tPackage);
        startActivity(intent);
    }
}

这是接收器活动

public class NActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        TPackage tPackage = intent.getParcelableExtra("parcel");
        HashMap<String, Channel> hashMap = tPackage.getList();
        Log.d("MyTag", "cost : " + tPackage.getCost() + " name: " + tPackage.getName()
                + " total channels: " + tPackage.getTotal_channels() + " list count: " + hashMap.size());
    }
}

Logcat输出

09-13 21:57:20.578: DEBUG/MyTag(1478): cost : 10 name: hello total channels: 123 list count: 0

答案 1 :(得分:0)

您可能需要在createFromParcel和writeToParcel中实际放置一些东西(比如对象的内容)