6

I'm trying to make my class parcelable, but it has a list of enums inside.

I've already seen how to do this with single enums here...

How could I do this?

Thanks! ;)

mvalencaa
  • 451
  • 1
  • 6
  • 16
  • 2
    If you can make a parcelable enum, then see [this question](http://stackoverflow.com/questions/7042272/how-to-properly-implement-parcelable-with-an-arraylistparcelable?rq=1) to make it work with the list. – Geobits Feb 22 '13 at 03:28

1 Answers1

14

Ok, I solved this just using the information at the link cited before.

That was what I did:

public enum Improvement {ENUM1, ENUM2, etc}

public void writeToParcel(Parcel dest, int flags) {
    ...
    List<String> improvementStrings = new ArrayList<String>();
    for (Improvement improvement : improvements) {
        improvementStrings.add(improvement.name());
    }
    dest.writeList(improvementStrings);
}

public void readFromParcel(Parcel in) {
    ...
    List<String> improvementStrings = new ArrayList<String>();
    in.readList(improvementStrings, null);
    for (String improvementString : improvementStrings) {
        improvements.add(Improvement.valueOf(improvementString));
    }
}
mvalencaa
  • 451
  • 1
  • 6
  • 16
  • 11
    It's probably faster to serialize `improvement.ordinal()`, which is an int, rather than `improvement.name()`. This avoids repeating the same set of strings over and over. Then, on the receiving end, `Improvement value = Improvement.values()[ordinal]` (and you can cache `Improvement.values()` in a local variable). – Jon O Feb 22 '13 at 14:14