Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an LinkedHashMap<Object,String> from one activity to another

I am facing an issue while passing a LinkedHashMap from one Activity to another. I referred all the related post by none of them could solve my problem. Please help me out.

Activity 1:

Intent mapIntent = new Intent(this,GMap.class);
LinkedHashMap<TravelMode, String> polyPoints=(LinkedHashMap<TravelMode, String>) gData.values().toArray()[0];
mapIntent.putExtra(EXTRA_MESSAGE, polyPoints);
startActivity(mapIntent);

Activity 2:

LinkedHashMap<Object,String>polypoint = (LinkedHashMap<Object, String>)poly.getSerializableExtra(EXTRA_MESSAGE);

This is the error I m getting while doing this operation.

Error:

 ClassCastException: Cannot cast java.util.HashMap (id=830032266720) to java.util.LinkedHashMap          

Class TravelMode:

  class TravelMode implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public String travelMode;

    /**
     * @return the travelMode
     */
    public String getTravelMode() {
        return travelMode;
    }

    /**
     * @param travelMode the travelMode to set
     */
    public void setTravelMode(String travelMode) {
        this.travelMode = travelMode;
    }

    public TravelMode(String travelMode) {
        super();
        this.travelMode = travelMode;
    }
}

I tried all the possibilities of retrieving like this one below, But still I getting the same error :(

HashMap<?,?>hashPoly= (HashMap<?, ?>)poly.getSerializableExtra(EXTRA_MESSAGE);
LinkedHashMap<TravelMode, String> polypoint= ((LinkedHashMap<TravelMode, String>)hashPoly);
like image 346
Sarath Subu Avatar asked Nov 02 '22 05:11

Sarath Subu


1 Answers

You can't directly cast from a HashMap to a LinkedHashMap. You should be able to do this instead:

LinkedHashMap<Object,String>polypoint = new LinkedHashMap<Object, String>();

and then add the previous HashMap with putAll:

polypoint.putAll(polyPoints);
like image 176
Martin Dinov Avatar answered Nov 15 '22 05:11

Martin Dinov