0

I have found a few simple examples of how to do this with an object that contains primitive types. I am working with a class that contains an array of objects as one of its members and several enumeration member variables. How would I store each object? I assume I would have to make both implement Parcelable. Are there any examples out there or others who have dealt with this problem?

How to send objects through bundle

Community
  • 1
  • 1

2 Answers2

1

i hope JSONObject will help you in this . you can put other object in to one json object by using put method ... and you can send that object by writing it in to any .txt or .json file and you can just parse that file and get all those object that you have write in to file

Vishal Mokal
  • 792
  • 1
  • 5
  • 22
  • Why would you use json to serialise the data ? simply using Parcelable is sufficient – Benoit Apr 22 '13 at 19:50
  • i dont have much idea about Parcelable but as question asked he wants to store each object thats why i suggested jsonobject where as he can store all member object in to one object and just write that object to file and use when ever he wants – Vishal Mokal Apr 22 '13 at 20:04
  • 1
    Well his question is probably not well written but his title says he wants to send it via a Bundle. Of course JSON would work but it's not the Android way. – Benoit Apr 22 '13 at 20:08
  • I already posted something but I guess it wasn't enough so I post a small easy sample too – Benoit Apr 22 '13 at 21:07
1

The Parcel interface gives you different possibilities for your array of objects

writeParcelableArray(T[] value, int parcelableFlags)
writeStringArray(String[] val)
writeStringList(List<String> val)

readParcelableArray(ClassLoader loader)
readStringList(List<String> list)
readStringArray(String[] val)

And for the Enum's you can either save the name and recreate it later using

readString()
writeString(String val)

Or getting the enum value and using

readInt()
writeInt(Int val)

Small code sample

public class Tag implements Parcelable {

private long id;
private String title;

// ... getter & setters & constructor ...

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

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeLong(id);
    out.writeString(title);
}

public static final transient Parcelable.Creator<Tag> CREATOR = new Parcelable.Creator<Tag>() {
    public Tag createFromParcel(Parcel in) {
        return new Tag(in);
    }

    public Tag[] newArray(int size) {
        return new Tag[size];
    }
};

protected Tag(Parcel in) {
    readFromParcel(in);
}

protected final void readFromParcel(Parcel in) {
    id = in.readLong();
    title = in.readString();
}
}
Benoit
  • 4,549
  • 3
  • 28
  • 45