我有這個持有我的JSON索引的ConstantData類,我需要在具有額外功能的活動之間傳遞它們。但在此之前,我必須首先實現Parcelable到這個類的對象。Android:如何實現Parcelable到我的對象?
我的問題是,我應該如何在這裏聲明我的類中的對象,並將每個對象放在一個變量中?
我是新手,現在我完全無能爲力。謝謝。隨意修改我的代碼如下。
ConstantData.java
public class ConstantData{
public static String project_title = "project title";
public static String organization_title = "organization title";
public static String keyword = "keyword";
public static String short_code = "short code";
public static String project_description = "description";
public static String smallImageUrl = "smallImageUrl";
public static String bigImageUrl = "bigImageUrl";
public static String price= "price";
public static String country= "country";
public static ArrayList<Project> projectsList = new ArrayList<Project>();
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(project_title);
out.writeString(organization_title);
out.writeString(keyword);
out.writeString(short_code);
out.writeString(project_description);
out.writeString(smallImageUrl);
out.writeString(bigImageUrl);
out.writeString(price);
out.writeString(country);
}
public static final Parcelable.Creator<ConstantData> CREATOR
= new Parcelable.Creator<ConstantData>() {
public ConstantData createFromParcel(Parcel in) {
return new ConstantData(in);
}
public ConstantData[] newArray(int size) {
return new ConstantData[size];
}
};
private ConstantData(Parcel in) {
project_title = in.readString();
organization_title = in.readString();
keyword = in.readString();
short_code = in.readString();
project_description = in.readString();
smallImageUrl = in.readString();
bigImageUrl = in.readString();
price = in.readString();
country = in.readString();
}
}
如果我的問題不夠清楚,你可以看一下這個問題:How to send an object from one Android Activity to another using Intents?
在那裏,他寫道:myParcelableObject
,我只是不知道如何製作可分類的對象。
編輯 Project.java
public class Project {
public String project_title;
public String organization_title;
public String keyword;
public String short_code;
public String project_description;
public String smallImageUrl;
public String bigImageUrl;
public String price;
public String country;
}
解決方案裏面:http://stackoverflow.com/questions/4049627/parcelable-and-inheritance-in-android – 2011-05-02 08:04:54
謝謝!所以在我的情況下,我只需要'公共抽象類A實現Parcelable'吧?我不需要'公共課B延伸A',我是否正確? – hectichavana 2011-05-02 08:13:47
有一件事沒有提到的是,如果您的數據已經作爲JSON/XML對象,並且已經有了解析器,那麼通過JSON或XML對象有時候會更容易,如果您沒有傳遞超過1-2的數據次 – 2012-10-11 14:51:27