javaandroidhashmapandroid-widget

Android - Get value from HashMap


I have tried to search on HashMap in Android, but getting problem:

Consider this example:

HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");

now I want to iterate it and get the value of each color and want to display in "Toast". how do I display it?


Solution

  • Iterator myVeryOwnIterator = meMap.keySet().iterator();
    while(myVeryOwnIterator.hasNext()) {
        String key=(String)myVeryOwnIterator.next();
        String value=(String)meMap.get(key);
        Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
    }