これは、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];
}
};
}
[編集済み] これは送信者のアクティビティです
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