嘿伙计们有很多麻烦让下面的代码工作。我正在尝试设置一个名为Topic
的parcelable对象数组,每个对象包含3个字符串和一个Issue
对象的ArrayList,这些对象是通过解析XML文件创建的。
问题是当我通过以下意图发送特定的Topic
对象时。
Intent intent = new Intent(context, ListIssues.class);
intent.putExtra("topic", topicObject);
context.startActivity(intent)`
并在ListIssues.class
getIntent().getExtras().getParcelable("topic");
我在活动OnCreate()
方法上收到了正确的结果。然后我创建以下Fragment
以显示Issue
列表对象。
public static SymptomFragment newInstance(ArrayList<IssueParcel> mIssues) {
SymptomFragment fragment = new SymptomFragment();
Bundle args = new Bundle();
args.putParcelableArray("topic", mIssues);
fragment.setArguments(args);
return fragment;
}
public SymptomFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
Array = getArguments().getParcelableArray("topic");
}
//Null when calling
}
我收到了Issue
个对象的不正确存储/创建的arraylist。字符串(title
,description
,image_url
)很好,但ArrayList<Issue>
包含有效的数组大小,但Issue
个对象本身为null或不正确地创建(某些子字符串是错误的);
public class Topic implements Parcelable {
String title;
String description;
String image_url;
ArrayList<Issue> issues = new ArrayList<>();
public Topic() {
}
public Topic(Parcel in) {
this.title = in.readString();
this.description = in.readString();
this.image_url = in.readString();
in.readList(this.issues, Issue.CREATOR);
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setImageUrl(String image_url) {
this.image_url = image_url;
}
public void setIssues(ArrayList<Issue> issues) {
this.issues = issues;
}
public String getTitle() { return this.title; }
public String getDescription() { return this.description; }
public String getImageUrl() { return this.image_url; }
public ArrayList<Issue> getIssues() { return this.issues; }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getTitle());
dest.writeString(getDescription());
dest.writeString(getImageUrl());
/** Think this could be the culprit */
dest.writeList(getIssues());
}
public static final Parcelable.Creator<Topic> CREATOR = new Parcelable.Creator<Topic>() {
public Topic createFromParcel(Parcel in) {
return new Topic(in);
}
@Override
public Topic[] newArray(int size) {
return new Topic[size];
}
};
}
public class Issue implements Parcelable {
private String connectedtype;
private String symptom;
private String problem;
private String solution;
private String comments;
public Issue() {
}
public Issue(Parcel in) {
this.connectedtype = in.readString();
this.symptom = in.readString();
this.problem = in.readString();
this.solution = in.readString();
this.comments = in.readString();
}
public void setConnectionType(String connectedtype) {
this.connectedtype = connectedtype;
}
public void setSymptom(String symptom) {
this.symptom = symptom;
}
public void setProblem(String problem) {
this.problem = problem;
}
public void setSolution(String solution) {
this.solution = solution;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getConnectionType() {
return this.connectedtype;
}
public String getSymptom() {
return this.symptom;
}
public String getProblem() {
return this.problem;
}
public String getSolution() {
return this.solution;
}
public String getComments() {
return this.comments;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getConnectionType());
dest.writeString(getSymptom());
dest.writeString(getProblem());
dest.writeString(getSolution());
dest.writeString(getComments());
}
public static final Parcelable.Creator<Issue> CREATOR = new Parcelable.Creator<Issue>() {
@Override
public Issue createFromParcel(Parcel source) {
return new Issue(source);
}
@Override
public Issue[] newArray(int size) {
return new Issue[size];
}
};
}
public ArrayList<Topic> parseXML() throws XmlPullParserException, IOException {
XmlPullParser xmlPullParser;
XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
xmlPullParser = xmlPullParserFactory.newPullParser();
xmlPullParser.setInput(openStream());
int eventType = xmlPullParser.getEventType();
boolean completed = false;
Issue mIssue = new Issue();
ArrayList<Issue> mIssues = new ArrayList<>();
Topic mTopic = new Topic();
ArrayList<Topic> mTopics = new ArrayList<>();
while (eventType != XmlPullParser.END_DOCUMENT && !completed) {
String tagName = xmlPullParser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if (tagName.equals(topic)) {
mTopic = new Topic();
}
if (tagName.equals(title)) {
mTopic.setTitle(xmlPullParser.nextText());
}
if (tagName.equals(description)) {
mTopic.setDescription(xmlPullParser.nextText());
}
if (tagName.equals(image_url)) {
mTopic.setImageUrl(xmlPullParser.nextText());
}
if (tagName.equals(issues)) {
mIssues = new ArrayList<Issue>();
}
if (tagName.equals(issue)) {
mIssue = new Issue();
}
if (tagName.equals(connection_type)) {
mIssue.setConnectionType(xmlPullParser.nextText());
}
if (tagName.equals(symptom)) {
mIssue.setSymptom(xmlPullParser.nextText());
}
if (tagName.equals(problem)) {
mIssue.setProblem(xmlPullParser.nextText());
}
if (tagName.equals(solution)) {
mIssue.setSolution(xmlPullParser.nextText());
}
if (tagName.equals(comments)) {
mIssue.setComments(xmlPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if (tagName.equals(root)) {
completed = true;
} else if (tagName.equals(issue)) {
mIssues.add(mIssue);
} else if (tagName.equals(issues)) {
mTopic.setIssues(mIssues);
} else if (tagName.equals(topic)) {
mTopics.add(mTopic);
}
break;
}
eventType = xmlPullParser.next();
}
closeStreams();
return mTopics;
}
答案 0 :(得分:0)
因此,将putParcelableArray("topic", mIssues)
更改为putParcelableArrayList("topic", mIssues)
并通过getParcelableArrayList()
而不是getParcelableArray()
进行检索,问题已得到解决。
然后我可以用in.readTypedList(x, y)