我试图将嵌套对象的arraylist从适配器传递给活动,并且此活动不是从中创建适配器的活动。我能够传递整数或任何其他数据,但对象的arraylist为null。
public class Rate implements Parcelable{
public final static Parcelable.Creator<Rate> CREATOR = new Creator<Rate>() {
@SuppressWarnings({
"unchecked"
})
public Rate createFromParcel(Parcel in) {
return new Rate(in);
}
public Rate[] newArray(int size) {
return (new Rate[size]);
}
};
@SerializedName("header")
@Expose
private String header;
@SerializedName("body")
@Expose
private List<Body> body = null;
@SerializedName("footer")
@Expose
private String footer;
@SerializedName("type")
@Expose
private String rateCardType;
@SerializedName("rules")
@Expose
private List<Rule> rateRules = null;
protected RateCard(Parcel in) {
this.header = ((String) in.readValue((String.class.getClassLoader())));
in.readList(this.body, (Body.class.getClassLoader()));
this.footer = ((String) in.readValue((String.class.getClassLoader())));
this.rateCardType = ((String) in.readValue((String.class.getClassLoader())));
in.readList(this.rateCardRules, (Rule.class.getClassLoader()));
}
public Rate() {
}
public List<Rule> getRateCardRules() {
return rateCardRules;
}
public void setRateCardRules(List<Rule> rateCardRules) {
this.rateCardRules = rateCardRules;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public List<Body> getBody() {
return body;
}
public void setBody(List<Body> body) {
this.body = body;
}
public String getFooter() {
return footer;
}
public void setFooter(String footer) {
this.footer = footer;
}
public String getRateCardType() {
return rateCardType;
}
public void setRateCardType(String rateCardType) {
this.rateCardType = rateCardType;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(header);
dest.writeList(body);
dest.writeValue(footer);
dest.writeValue(rateCardType);
dest.writeList(rateCardRules);
}
public int describeContents() {
return 0;
}
}
我想将Rule对象arraylist传递给另一个活动。我的规则 课程如下所示
public class Rule implements Parcelable{
public final static Parcelable.Creator<Rule> CREATOR = new Creator<Rule>() {
@SuppressWarnings({
"unchecked"
})
public Rule createFromParcel(Parcel in) {
return new Rule(in);
}
public Rule[] newArray(int size) {
return (new Rule[size]);
}
};
@SerializedName("rule")
@Expose
private DataRows ruleData;
@SerializedName("rule_conditions")
@Expose
private List<DataRows> ruleConditions;
@SerializedName("slots")
@Expose
private List<DataRows> slots;
@SerializedName("header")
@Expose
private DataRows header;
protected Rule(Parcel in) {
in.readList(this.ruleConditions, (DataRows.class.getClassLoader()));
in.readList(this.slots, (DataRows.class.getClassLoader()));
this.ruleData = in.readParcelable((DataRows.class.getClassLoader()));
this.header = in.readParcelable((DataRows.class.getClassLoader()));
}
public Rule() {
}
public DataRows getRuleData() {
return ruleData;
}
public void setRuleData(DataRows ruleData) {
this.ruleData = ruleData;
}
public List<DataRows> getRuleConditions() {
return ruleConditions;
}
public void setRuleConditions(List<DataRows> ruleConditions) {
this.ruleConditions = ruleConditions;
}
public List<DataRows> getSlots() {
return slots;
}
public void setSlots(List<DataRows> slots) {
this.slots = slots;
}
public DataRows getHeader() {
return header;
}
public void setHeader(DataRows header) {
this.header = header;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(ruleConditions);
dest.writeList(slots);
dest.writeParcelable(ruleData, flags);
dest.writeParcelable(header, flags);
}
public int describeContents() {
return 0;
}
}
我的DataRows类
public class DataRows implements Parcelable{
public final static Parcelable.Creator<DataRows> CREATOR = new Creator<DataRows>() {
@SuppressWarnings({
"unchecked"
})
public DataRows createFromParcel(Parcel in) {
return new DataRows(in);
}
public DataRows[] newArray(int size) {
return (new DataRows[size]);
}
};
@SerializedName("key")
@Expose
private String key;
@SerializedName("value")
@Expose
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
protected DataRows(Parcel in) {
this.key = ((String) in.readValue((String.class.getClassLoader())));
this.value = ((String) in.readValue((String.class.getClassLoader())));
}
public DataRows() {
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(key);
dest.writeValue(value);
}
public int describeContents() {
return 0;
}
}
这就是我试图传递值
的方法
Intent intent = new Intent(context, RulesDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("rules", (ArrayList<? extends Parcelable>) ruleList);
bundle.putInt("current_rule_index", ruleIndex);
startActivity(context, bundle, intent);
我对'&#34;规则&#34;在目标活动中。这里出了什么问题。有人可以帮忙。
答案 0 :(得分:1)
对于这些问题,我总是喜欢使用Gson库。这是我的解决方案。
导入gson库。
compile 'com.google.code.gson:gson:2.7'
使用适配器类中的gson将嵌套的Arraylist转换为字符串。
Gson gson = new Gson();
Intent intent = new Intent(context, RulesDetailActivity.class);
intent.putExtra("rules", gson.toJson(ruleList);
startActivity(intent);
然后在RulesDetailActivity类中执行以下操作:
Gson gson = new Gson();
String gson = getIntent().getStringExtra("rules");
Type type = new TypeToken<List<Rule>>() {
}.getType();
ArraList<Rule> rules = gson.fromGson(gson, type);