I am trying to pass LinkedHashMap between activities, my code is like this
Activity A
LinkedHashMap mLinkedHashMap = new LinkedHashMap<String, Object>();
// setting data in map
Bundle bundle = new Bundle();
bundle.putSerializable("OBJECTS_LINKED_HASHMAP", mLinkedHashMap);
Intent intent = new Intent(this, ActivityB.class);
intent.putExtras(bundle);
startActivity(intent);
in Activity when I receive bundle object I am getting "Class cast exception" error message
Activity B
Bundle bundle = this.getIntent().getExtras();
LinkedHashMap mLinkedHashMap = new LinkedHashMap<String, Object>();
mLinkedHashMap = (LinkedHashMap<String, Object>) bundle.getSerializable("OBJECTS_LINKED_HASHMAP");
Getting class cast exceptions
ClassCastException: java.util.HashMap cannot be cast to LinkedHashMap
I have checked documentation LinkedHashMap also implementing Serializable interface.
I am using LinkedHashMap because I want to maintain object order, they way they inserted I want back in order.
How to pass LinkedHashMap between activities ?
Try GSON
for change :)
Download gson.jar from this link
And add gson-2.2.2.jar file in your project.
Now pass your LinkedHashMap to another activity using GSON
like this(modified this below code as per your need):
MainActivity:::
public class MainActivity extends Activity {
ObjectClass obj=new ObjectClass();
LinkedHashMap<String, ObjectClass> mLinkedHashMap = new LinkedHashMap<String, ObjectClass>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
obj.id=1;
obj.name="hello";
mLinkedHashMap.put("test", obj);
Gson gson = new Gson();
String list = gson.toJson(mLinkedHashMap);
Intent intent = new Intent(this, secondActivity.class);
intent.putExtra("list", list);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
secondActivity:::
public class secondActivity extends Activity {
LinkedHashMap<String, ObjectClass> mLinkedHashMap = new LinkedHashMap<String, ObjectClass>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str= getIntent().getStringExtra("list");
Gson gson = new Gson();
Type entityType = new TypeToken< LinkedHashMap<String, ObjectClass>>(){}.getType();
mLinkedHashMap = gson.fromJson(str, entityType);
ObjectClass obj = mLinkedHashMap.get("test");
Log.i("list", ""+obj.id);
}
}
Worked for me. Hope this will help.
And Here is my object class for reference.
public class ObjectClass {
public int id;
public String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
How to add gson-2.2.2.jar file.
1) copy your gson-2.2.2.jar from downloaded folder "google-gson-2.2.2" .
2) paste it to your project's asset folder.
3) now go to your project buildpath by right clicking on the your project>Build Path>Configure Build Path..
4) It will open one dialog select java build path from right menu and go to library tag then
Click on "Add Jars..." button as you can see in below image
it will open another dialog to add jar. here select the gson-2.2.2.jar that we added in project's asset folder(step 2).
It will add jar to your project(I already added it in my project as you can see in below image)
5)Now select Order and Export tag and select your gson-2.2.2.jar(see below image).
6)Press OK and now you can use GSON
in your project
A LinkedHashMap<> is not Parcelable or Serializable
EDIT :
Check this :
serialize/deserialize a LinkedHashMap (android) java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With