I have tried to search on HashMap in Android, but getting problem:
Consider this example:
HashMap meMap=new HashMap
for (Object key : meMap.keySet()) {
String value=(String)meMap.get(key);
Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}
HashMap<String, String> meMap = new HashMap<String, String>();
meMap.put("Color1", "Red");
meMap.put("Color2", "Blue");
meMap.put("Color3", "Green");
meMap.put("Color4", "White");
Iterator myVeryOwnIterator = meMap.values().iterator();
while(myVeryOwnIterator.hasNext()) {
Toast.makeText(getBaseContext(), myVeryOwnIterator.next(), Toast.LENGTH_SHORT).show();
}
this work for me:
HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");
Iterator iterator = meMap.keySet().iterator();
while( iterator. hasNext() )
{
Toast.makeText(getBaseContext(), meMap.get(iterator.next().toString()),
Toast.LENGTH_SHORT).show();
}
Note: If you know Key, use this code
String value=meMap.get(key);
Iterator iterator = meMap.keySet().iterator();
while(iterator.hasNext() ){
//get key
String key=(String)iterator.next();
//toast value
Toast.makeText(getBaseContext(),""+(String)meMap.get(key),Toast.LENGTH_SHORT).show();
}
HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Color1","Red");
meMap.put("Color2","Blue");
meMap.put("Color3","Green");
meMap.put("Color4","White");
Iterator iterator = meMap.keySet().iterator();
while( iterator. hasNext() ){
Toast.makeText(getBaseContext(), meMap.get(iterator.next().toString()),
Toast.LENGTH_SHORT).show();
}