将对象的嵌套数组列表从适配器传递到活动
Posted
技术标签:
【中文标题】将对象的嵌套数组列表从适配器传递到活动【英文标题】:Passing nested arraylist of object from adapter to activity 【发布时间】:2018-10-23 01:42:03 【问题描述】:我试图将嵌套对象的数组列表从适配器传递给活动,而该活动不是创建适配器的活动。我可以将整数或任何其他数据传递给它,但对象的数组列表为空。
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;
我想将规则对象数组列表传递给另一个活动。我的规则 类如下图
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);
我的目标活动中的“规则”为空。这里出了什么问题。有人可以帮忙吗?
【问题讨论】:
【参考方案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);
【讨论】:
以上是关于将对象的嵌套数组列表从适配器传递到活动的主要内容,如果未能解决你的问题,请参考以下文章
如何将值从基本适配器传递到 Android 中的 Activity
如何使用适配器为recyclerview从字符串数组列表中的元素获取字符串