0

I have an ArrayList full of custom objects. I need to save this ArrayList to a Bundle and then retrieve it later.

Having failed with both Serializable and Parcelable, I'm now simply trying to somehow save the objects that are associated with the indexes in the ArrayList, then checking these when restoring the Bundle and adding the objects back in.

What I have is something like this:

When saving the Bundle:

    //Create temporary array of the same length as my ArrayList
    String [] tempStringArray = new String[myList.size()];

    //Convert the enum to a string and save it in the temporary array
    for (int i = 0; i<myList.size();i++){
                tempStringArray [i] = myList.get(i).getType();  //returns the enum in string form
    }

    //Write this to the Bundle
    bundle.putStringArray("List", tempStringArray);

So I now have an array of strings representing the enum types of the objects that were originally in the ArrayList.

So, when restoring the Bundle, what I'm trying is something like this:

//Temporary string array
String[] tempStringArray = savedState.getStringArray("List");

//Temporary enum array
ObjectType[] tempEnumArray = new ObjectType[tempStringArray.length];

for (int i = 0; i<tempStringArray.length;i++){
    tempEnumArray[i]=ObjectType.valueOf(tempEnemies[i]);
}

So, now I have the enum type of each item that was originally in the ArrayList.

What I'm now trying to do now, is something like (would go inside the for loop above):

myList.add(tempEnumArray[i].ObjectTypeThisEnumRefersTo());

Obviously the "ObjectTypeThisEnumRefersTo()" method above doesn't exist but this is ultimately, what I'm trying to find out. Is this possible or perhaps there is some other way of doing this?

Zippy
  • 3,826
  • 5
  • 43
  • 96
  • Are you talking about Java enums, or "enum" in some broader sense? What kind of objects does myList hold initially? – JHH Mar 13 '15 at 20:56
  • Hi @JHH, I'm asking about Java enums here. The original arraylist holds custom objects (in my case they are objects of the type 'Enemy' as this is a game) Hope this helps. – Zippy Mar 13 '15 at 20:59
  • So, forgive me if I'm misunderstanding, does myList hold different values of one single enum called Enemy? And you want to recreate the enum *values* from strings? Because at first I thought you had objects of different classes and needed to determine the classes. If you need the values from their respective strings, Enemy.valueOf(String) is your friend. – JHH Mar 13 '15 at 21:13
  • No @JHH, all objects in the list are instances of the 'Enemy' class - so I'd do this: bee = new Enemy(EnemyType.Bee); and then spider = new Enemy(EnemyType.spider); So, I need to add the items are they were when the Bundle was saved - so myList.add(bee), myList.add(spider). But obviously the have to go back in the order they were originally. Hope this makes sense! – Zippy Mar 13 '15 at 21:17

1 Answers1

1

To get an enum value of the enum type Enemy from a string, use

Enemy.valueOf(String).

Enemy.valueOf("SPIDER") would return Enemy.SPIDER, provided your enum looks like

enum Enemy { SPIDER,  BEE};

EDIT: It turns out Zippy also had a fixed set of Enemy objects, each mapped to each value of EnemyType, and needed a way to find an Enemy from a given EnemyType. My suggestion is to create a

HashMap<EnemyType, Enemy> 

and put all the objects in there upon creation, then at deserialization convert strings to enum values and enum values to Enemy objects using the hashmap.

It later occurred to me though that depending on how much logic you have in Enemy, you might want to consider scrapping either Enemy or EnemyType and combine them into one parameterized enum, similar to the example with Planet over here:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html That would spare you from having to go two steps from a string to your final object and simplify things a bit as you wouldn't need any hashmap after all.

JHH
  • 8,567
  • 8
  • 47
  • 91
  • Hi @JHH, I'm already doing that (you can see in the for-loop above), what I can't then do is say something like this. myList.add[Enemy.valueOf(String)]; because the arrayList myList is expecting an object of type 'Enemy' rather than an enum. Sorry if I wasn't clear in explaining what I'm after. So, having managed to restore the enum, I now need to get the object that was originally associated with the enum so I can add it back into the ArrayList, :-) – Zippy Mar 13 '15 at 21:23
  • In other words, I need a way of saying 'If this enum is Bee, then myList.add(bee);' or 'if this enum is Spider, then myList.add(spider);' - I could do this with a switch statement but that wouldn't be so good. – Zippy Mar 13 '15 at 21:28
  • I'm really confused now. :-) So, Enemy is not actually the enu, EnemyType is. And Enemy is an ordinary class having a reference to an EnemyType value. In your example above, you said you created Enemy objects such as bee = new Enemy(EnemyType.BEE). Does Enemy now hold more data than this reference? Do you need the Enemy class wrapping your enum values at all? And can't you simply recreate your Enemy objects through new Enemy(EnemyType.valueOf(String) then? – JHH Mar 13 '15 at 21:35
  • I am not sure how a switch would help, as I assume the objects you created before storing to the bundle (bee, spider....) are not around any more? Don't you simply want to create new, equivalent, objects..? – JHH Mar 13 '15 at 21:37
  • Me too! The object would be around when restoring because they are all re-created when the app is re-launched. The Enemy objects do contain more information - like position, acceleration, current animation frame etc..... I use the enum for various things like determining what type of enemy this is within the game's logic. So, all objects do exist at the point of restoring the Bundle. I just need to be able to add them to the list (which is also recreated, but empty) in the order they were originally in. Seems such a simple thing to do but I've been at this now for 2 days solid so far! – Zippy Mar 13 '15 at 21:43
  • Well since Enemy is not an enum and hence doesn't have any static built in way to map strings or enums to instances of itself, how about creating a HashMap when your app is created and store mappings between enum values and instances of Enemy there? Then look up your objects from it after restoring enums from the bundle. – JHH Mar 13 '15 at 21:53
  • This sounds interesting, but how would the HashMap persist after the app was killed? Would I need to save the HashMap to the Bundle also? Not really used HashMaps before TBH but will look into it. – Zippy Mar 13 '15 at 21:56
  • Well you said you are recreating all those bee, spider etc objects at app startup right? Because you said they would be available also after app killed and restarted when reading from the bundle. So just put them in the map directly after creation and keep a reference to the hashmap. – JHH Mar 13 '15 at 21:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72954/discussion-between-zippy-and-jhh). – Zippy Mar 13 '15 at 21:59
  • Hey @JHH Thanks so much for your suggestion of how to get the object type associated with the enum, it worked perfectly - if you wanna update your answer, I'll accept it - cheers! :-) – Zippy Mar 14 '15 at 01:40
  • 1
    Glad it worked out. I updated my answer, and also added one additional idea. – JHH Mar 14 '15 at 07:16