serialization - Android LinkedHashMap deserializing as HashMap, causes CCE error -


i'm attempting pass serialized linkedhashmap between activities, , getting confusing result/error when deserialize object.

i serialize object follows:

    bundle exdetails = new bundle();     linkedhashmap<string, exercise> exmap = new linkedhashmap<string,      exercise (workout.getexercises());      exdetails.putstring(workoutname, workout.getworkoutname());     exdetails.putserializable(workout.getworkoutname(), exmap);      icomplete.putextra("wname", exdetails);     startactivity(icomplete); 

that seems work fine, problem shows in next activity:

    bundle exdetails =  getintent().getbundleextra("wname");     workoutname = exdetails.getstring(workoutname);     serializable edata = exdetails.getserializable(workoutname);      ex = new linkedhashmap<string, exercise>();     ex = (linkedhashmap<string, exercise>) edata; 

at point, deserialized object (edata) contains hashmap object (not linkedhashmap), , gives me

java.lang.classcastexception:java.util.hashmap cannot cast java.util.linkedhashmap 

on last line. i've verified debugger bundle (in second activity) contains hashmap, instead of linkedhashmap (as i'm assuming should). should mention need maintain order in entries added map, hence usage of linkedhashmap. entries printed, , order important output.

questions: doing wrong in particular, or problem due bugs linkedhashmap's serialization? i've noticed few similar threads, seem speak of being ongoing problem several of map implementations. didn't answer problem directly though.

if latter, there workaround isn't advanced (i'm not far beyond beginner level, i'm willing try things), or need bite bullet , work other linkedhashmap?

p.s. tried include relevant, can add more code if left out important.

i went different approach: serialize (type of) map 2 arraylists: 1 containing keys , other 1 containing values. way, order of map entries (important in linkedhashmap) kept.

each key/value implements serializable. if know sure need strings or 1 particular type of map, should easy convert following generic code scenario needed simplifies complexity.

map -> 2 arraylists:

public static <k extends serializable, v extends serializable> pair<arraylist<k>, arraylist<v>> convertmaptoarrays(@nonnull map<k, v> map) {         final set<map.entry<k, v>> entries = map.entryset();         final int size = entries.size();         final arraylist<k> keys = new arraylist<>(size);         final arraylist<v> values = new arraylist<>(size);         (map.entry<k, v> entry : entries) {             keys.add(entry.getkey());             values.add(entry.getvalue());         }         return new pair<>(keys, values); } 

2 arraylists -> map of specific type

 public static <k extends serializable, v extends serializable> map<k, v> convertarraystomap(@nonnull arraylist<k> keys, @nonnull arraylist<v> values, @nonnull class<? extends map<k, v>> mapclass) {         if (keys.size() != values.size()) {             throw new runtimeexception("keys , values must have same number of elements");         }         final int size = keys.size();         map<k, v> map;         try {             final constructor<? extends map<k, v>> constructor = mapclass.getconstructor(integer.type);             map = constructor.newinstance(size);         } catch (exception nse) {             throw new runtimeexception("map constructor accepts initial capacity not found.");         }          (int = 0; < size; i++) {             final k key = keys.get(i);             final v value = values.get(i);             map.put(key, value);         }         return map;     } 

helpers android's bundle:

 public static <k extends serializable, v extends serializable> void savemaptobundleasarrays(@nonnull map<k, v> map, @nonnull string key, @nonnull bundle bundle) {         final pair<arraylist<k>, arraylist<v>> maptoarrays = convertmaptoarrays(map);         final string keyforkeys = key + "_keys";         final string keyforvalues = key + "_values";         bundle.putserializable(keyforkeys, maptoarrays.first);         bundle.putserializable(keyforvalues, maptoarrays.second);     }  public static map<serializable, serializable> loadmapfrombundle(@nonnull bundle bundle, @nonnull string key, @nonnull class<? extends map<serializable, serializable>> mapclass) {         final string keyforkeys = key + "_keys";         final string keyforvalues = key + "_values";         final arraylist<serializable> keys = (arraylist<serializable>) bundle.getserializable(keyforkeys);         final arraylist<serializable> values = (arraylist<serializable>) bundle.getserializable(keyforvalues);         return convertarraystomap(keys, values, mapclass);     } 

usage:

savemaptobundleasarrays(mmodelevolution, key_model_data, bundle);  class<linkedhashmap<serializable, serializable>> linkedhashmapclazz =                 (class<linkedhashmap<serializable, serializable>>) new linkedhashmap<string, string>().getclass(); mmodelevolution = (linkedhashmap) objectutils.loadmapfrombundle(bundle, key_model_data, linkedhashmapclazz); 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -