Reputation: 2763
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.
Upvotes: 2
Views: 1720
Reputation: 1020
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);
Upvotes: -2
Reputation: 1066
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.
Upvotes: 0
Reputation: 15476
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.
Upvotes: 4