0

Hey guys im having alot of troubles getting the following code to work. I am trying to setup an array of parcelable objects named Topic which each contain 3 strings and an ArrayList of Issue objects that are created by parsing an XML file.

The issue is that when im sending a specific Topic object via the following intent.

Intent intent = new Intent(context, ListIssues.class);
intent.putExtra("topic", topicObject);
context.startActivity(intent)`

And receiving the extras in the ListIssues.class

getIntent().getExtras().getParcelable("topic");

I receive the proper result on the Activities OnCreate() method. I then am creating the following Fragment to display the Issue list objects.

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 
}

i am receiving a improperly stored/created arraylist of Issue objects. The strings (title,description,image_url) are fine, However the ArrayList<Issue> contains the valid array size, however the Issue objects themself are either null or were improperly created (certain child strings are wrong);

Topic

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];
    }
 };
}

Issue

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];
    }
  };
}

Creating the Objects

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;
}
Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
  • Where creating `Issue` and `Topic` class objects before adding in Bundle ? – ρяσѕρєя K Feb 20 '15 at 03:21
  • I came across a similar issue, and found that if I used an Array instead of an ArrayList, I could use writeTypedArray and createTypedArray. Just leaving this as a comment, since I don't know if you can or would want to convert your type. – WoogieNoogie Feb 20 '15 at 03:31
  • The initial `Topic` objects strings are used to display a list. Upon click im grabbing that specific object and passing it through the intent. – Jaison Brooks Feb 20 '15 at 03:41
  • I'll have to think about that @WoogieNoogie – Jaison Brooks Feb 20 '15 at 03:41
  • @JaisonBrooks: check `topic` before starting new Activity you are getting right values got `Issue` in log? – ρяσѕρєя K Feb 20 '15 at 03:44
  • 1
    You should be using `writeTypedList()`. The `writeList()` method only takes lists of [certain types](http://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object)). – Mike M. Feb 20 '15 at 04:12
  • @ρяσѕρєяK I check and the data is stored properly before the activity intent, its also looks like its received properly on the activity (w/ help from @Mike), ON this activity i now am creating a `Fragment` where i intend on displaying the list of `Issue`'s. See the update code for the fragment creation (this may be the problem) – Jaison Brooks Feb 20 '15 at 04:16
  • Thank you MikeM and ρяσѕρєяK. With your help i have resolved the issue. – Jaison Brooks Feb 20 '15 at 04:34

1 Answers1

0

So by changing putParcelableArray("topic", mIssues) to putParcelableArrayList("topic", mIssues) and retrieving it via getParcelableArrayList() instead of getParcelableArray() the issue has been resolved.

I can then update the Topic object with in.readTypedList(x, y)

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79