0

I have a Map which looks as follows:

 val myMap = mutableMapOf<String,String>() 

 myMap.put("A", "APPLE")
 myMap.put("B", "BALL")
 myMap.put("C", "CAT")
 myMap.put("D", "DOG")
 ...

When I print the map as follows, it prints it correctly:

println("MAP DATA -> $myMap")

//Output
MAP DATA -> [A = APPLE, B = BALL, C = CAT, D = DOG, ...]

But in my activity when I want to check if the value for a specific key is what it is, so that I can check a specific radio button, it doesn't work.

I tried the following:

 val option1 = myMap.get("A")
        
 if(option1 == "APPLE"){

    radioOption1.isChecked = true

  }

EDIT: The 2nd way I tried was this (in this case, the println text gets printed, but the radio button won't get checked.:

    if(myMap.getValue("A") == "APPLE"){
        println("getValue of MAP")
        radioOption1.isChecked = true
    }

When I try this it says : The key 'A' does not exist in the map.

I am struggling with it. How can I get the value of a specific key from the map so that I can check the relevant radioButton?

Any help or advice will be highly appreciated.

  • what do you get when you print `option1`? – Stachu Sep 30 '20 at 19:23
  • @Stachu when I print `option1` I get `null` –  Sep 30 '20 at 19:33
  • @Stachu I have added another way I tried it, which does print what's in the `if` statement, but won't check the radio button (Edited my code above) –  Sep 30 '20 at 19:45
  • as Patrick has said, the map seems to be populated after you to get the key to check radio button, hence the null value – Stachu Sep 30 '20 at 19:54

0 Answers0