0

What I want is to pass a list of list from one activity to another. Here is my approach.

Feeditem.java

public class FeedItem implements Parcelable {

private String  id,status, image, timeStamp, url;
private ArrayList<CommentItem> commentItems;

public FeedItem() {
}


protected FeedItem(Parcel in) {
    id = in.readString();
    status = in.readString();
    image = in.readString();
    timeStamp = in.readString();
    url = in.readString();
    if(commentItems!=null) {
        in.createTypedArrayList(CommentItem.CREATOR);
    }
}

public static final Creator<FeedItem> CREATOR = new Creator<FeedItem>() {
    @Override
    public FeedItem createFromParcel(Parcel in) {
        return new FeedItem(in);
    }

    @Override
    public FeedItem[] newArray(int size) {
        return new FeedItem[size];
    }
};

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getImge() {
    return image;
}

public void setImge(String image) {
    this.image = image;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getTimeStamp() {
    return timeStamp;
}

public void setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public ArrayList<CommentItem> getCommentItems() {
    return commentItems;
}

public void setCommentItems(ArrayList<CommentItem> commentItems)
{
    this.commentItems=commentItems;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(id);
    dest.writeString(status);
    dest.writeString(image);
    dest.writeString(timeStamp);
    dest.writeString(url);
    dest.writeTypedList(commentItems);
}}

CommentItem.java

public class CommentItem implements Parcelable{

private String  id,comment, from, timeStamp;

public CommentItem() {
}

protected CommentItem(Parcel in) {
    id = in.readString();
    comment = in.readString();
    from = in.readString();
    timeStamp = in.readString();
}


public static final Creator<CommentItem> CREATOR = new Creator<CommentItem>() {
    @Override
    public CommentItem createFromParcel(Parcel in) {
        return new CommentItem(in);
    }

    @Override
    public CommentItem[] newArray(int size) {
        return new CommentItem[size];
    }
};

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getFrom() {
    return from;
}

public void setFrom(String from) {
    this.from = from;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

public String getTimeStamp() {
    return timeStamp;
}

public void setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(id);
    dest.writeString(comment);
    dest.writeString(from);
    dest.writeString(timeStamp);
}}

ReceivingActivity.java

 ArrayList<CommentItem> commentItems;
    FeedItem feedItem;
    commentItems=new ArrayList<>();
    Bundle bundle=getIntent().getExtras();

    feedItem=bundle.getParcelable("status");

    if(feedItem.getCommentItems()!=null) {                //help here
        commentItems = feedItem.getCommentItems();
    }

SendingActivity.java

Button getComments=(Button)convertView.findViewById(R.id.commentsbutton);
    getComments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Intent i = new Intent(activity, CommentActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("status", item);  //item is feeditem 
                i.putExtras(bundle);
                activity.startActivity(i);

        }
    });

I am able to get all the attributes of the feeditem except the list which is giving me null in the receiving activity. Could someone please help me out with this. Thanks in advance.

1 Answers1

0

I think the problem is that you're only reading in the list if it is already non-null:

if(commentItems!=null) {
        in.createTypedArrayList(CommentItem.CREATOR);
    }

If it's null, which it will be at that point, it doesn't get set. Just remove the non-null check and I think it will work.

commentItems = in.createTypedArrayList(CommentItem.CREATOR);
nasch
  • 5,330
  • 6
  • 31
  • 52
  • i am using a getter to get the list from the feeditem, look closer.- I am using feeditem.getcommentitems instead of commentitems which I know is always null. – rohanagarwal94 Jul 25 '16 at 06:13
  • You changed `FeedItem` as I suggested? That's what I was talking about, not `ReceivingActivity`. – nasch Jul 25 '16 at 14:07