2

I have a very strange problem. All I want to do is pass a LinkedHashMap<String,HashMap<String,String>> (I am just experimenting here and the I would like the choice of data structure to remain unchanged regardless of performance overhead) object from one Activity to another via an Intent. I tried two approaches after initializing and populating my data structure:

 LinkedHashMap<String,HashMap<String,String>> mData = new LinkedHashMap<String,HashMap<String,String>>();
//insert data into LinkedHashMap

Approach 1:

 Intent intent = new Intent(this, TestActivity.class);             
 // as a Serializable
 intent.putExtra("DATA",mData)
 startActivity(intent);

Approach 2:

 Intent intent = new Intent(this, TestActivity.class);             
 Bundle bundle = new Bundle();
 bundle.putSerializable("LIST",mData);
 intent.putExtra("DATA",bundle)
 startActivity(intent);

And in the receiving Activity:

For Approach 1:

Intent intent = getIntent();
mData = (LinkedHashMap<String, HashMap<String, String>>) intent.getSerializableExtra("DATA");

For Approach 2:

Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("DATA");
mData = (LinkedHashMap<String, HashMap<String, String>>) bundle.getSerializable("LIST");

And I keep running into this issue:

 Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.LinkedHashMap
            at com.matchify.TestActivity.onCreate(TestActivity.java:56)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

What am I doing wrong ? Apologies if I have missed something very obvious.

user1841702
  • 2,683
  • 6
  • 35
  • 53
  • You aren't doing anything wrong. Android converts the `LinkedHashMap` to a `HashMap` when you call `purExtra()`. See http://stackoverflow.com/questions/4164050/sending-linkedhashmap-to-intent/38960732#38960732 and http://stackoverflow.com/questions/12300886/linkedlist-put-into-intent-extra-gets-recast-to-arraylist-when-retrieving-in-nex/12305459#12305459 for more details – David Wasser Aug 15 '16 at 19:03

3 Answers3

4

I checked LinkedHashMap's source code and found the data structure that stores iteration order is declared as transient:

transient LinkedEntry<K, V> header;

So even if LinkedHashMap is returned to the new Activity, the map would be broken as far as iteration is concerned, which may be the reason why HashMap is returned instead.

If you don't need guaranteed iteration order, just cast it as HashMap in the 2nd Activity, otherwise you may have to write your own Parcelable to preserve the ordering.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • Android converts the `LinkedHashMap` into a `HashMap` when adding it to the `Bundle`. It's an Android idiosyncrasy. The easiest way to get around this is to convert the `LinkedHashMap` into an ordered array of whatevers, which you add to the `Bundle`, then extract the array on the receiving end and reconstruct the `LinkedHashMap`. See http://stackoverflow.com/a/38960732/769265 and http://stackoverflow.com/questions/12300886/linkedlist-put-into-intent-extra-gets-recast-to-arraylist-when-retrieving-in-nex/12305459#12305459 for more details – David Wasser Aug 15 '16 at 19:00
0
Bundle bundle = new Bundle();
bundle.putSerializable("factorsDataMap", mData);
Intent intent = new Intent(MainActivity.this, MoodSummary.class); 
intent.putExtras(bundle);
startActivity(intent);

Try this, Hope it works. Tell me if you still have problem.

Ziem
  • 6,579
  • 8
  • 53
  • 86
akhil batlawala
  • 1,066
  • 1
  • 10
  • 30
-2

Try this hope you help!

Intent intent = new Intent(this, TestActivity.class);
intent.putExtra("hashmap", hashMap);
startActivity(intent);

//receiving Activity:

hashMap = (HashMap<String,String>)intent.getExtras().getSerializable("hashmap");
LinkedHashMap<String,String> linkedMap = new LinkedHashMap<>(hashMap);
Mohammad nabil
  • 1,010
  • 2
  • 12
  • 23