Possible implementation using ArrayList <custom-object>

I think this is just a stupid mistake, but ArrayList always ends up null . It drove me crazy, so I thought I would ask for help.

Object Class:

 import android.os.Parcel; import android.os.Parcelable; public class StoryTag implements Parcelable { private String tagTitle; private int occurrence; public StoryTag() { } public StoryTag(Parcel in) { tagTitle = in.readString(); occurrence = in.readInt(); } public String getTagTitle() { return tagTitle; } public void setTagTitle(String tagstring) { this.tagTitle = tagstring; } public int getOccurrence() { return occurrence; } public void setOccurrence(int occurrence) { this.occurrence = occurrence; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(tagTitle); dest.writeInt(occurrence); } public int describeContents() { return 0; } public static final Parcelable.Creator<StoryTag> CREATOR = new Parcelable.Creator<StoryTag>() { public StoryTag createFromParcel(Parcel in) { return new StoryTag(in); } public StoryTag[] newArray(int size) { return new StoryTag[size]; } }; } 

MainActivity :

 Intent tagIntent=new Intent(this,DisplayTagList.class); tagIntent.putExtra("taglist", taglist); startActivity(tagIntent); return true; 

Receiving Activity:

 Bundle storyTagBundle = getIntent().getExtras(); ArrayList<StoryTag> listoftags = storyTagBundle.getParcelable("taglist"); 

Thanks for the ton for any help you can offer. Pulling my hair here is for what I consider a minor mistake.

+4
source share
2 answers

The putExtra() and getSerializable() methods will store and retrieve the ArrayList<?> getSerializable() your custom objects without the need for an interface. (Your custom object class should implement the Serializable interface, though).

But in your case, you can just use putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) and getParcelableArrayListExtra(String name) .

+4
source

You can get a clear idea of ​​the implementation of ArrayList with Parcelable from this complete Example .

0
source

All Articles