Android - Get value from HashMap

前端 未结 9 1423
抹茶落季
抹茶落季 2020-11-29 16:59

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

Consider this example:

HashMap meMap=new HashMap

        
相关标签:
9条回答
  • 2020-11-29 17:20
    for (Object key : meMap.keySet()) {
            String value=(String)meMap.get(key);
            Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
        }
    
    0 讨论(0)
  • 2020-11-29 17:21
    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();
    }
    
    0 讨论(0)
  • 2020-11-29 17:24

    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();
    }
    
    0 讨论(0)
  • 2020-11-29 17:30

    Note: If you know Key, use this code

    String value=meMap.get(key);
    
    0 讨论(0)
  • 2020-11-29 17:31
     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();
    
                    }
    
    0 讨论(0)
  • 2020-11-29 17:34
    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();
    }
    
    0 讨论(0)
提交回复
热议问题