5

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);
Sarath Subu
  • 113
  • 9

2 Answers2

1

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);
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • 1
    Thanks. It solves the problem. But now the order is getting changed. But I want to retain the order in which the list is sent and that's the reason Y I was using linkedHashMap. But now in the second activity the order is changed. – Sarath Subu Feb 08 '14 at 11:08
  • 1
    The order is changed because Android converts the `LinkedHashMap` to a `HashMap` when you call `putExtra()`, thereby discarding all the ordering. – David Wasser Aug 15 '16 at 18:55
1

Android converts the LinkedHashMap to a HashMap when you call putExtra(). This discards the ordering, which is why you want to use a LinkedHashMap in the first place.

To get around this, you should convert the LinkedHashMap to an ordered array, and put the array in the Intent extras. On the receiving end you can then recreate the LinkedHashMap from the ordered array (if necessary), or just use the array as is.

See https://stackoverflow.com/a/38960732/769265 and LinkedList put into Intent extra gets recast to ArrayList when retrieving in next activity for more details.

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274