0

I am trying to retrieve a LinkedHashMap key value, but I do not find any coherent solution for my problem, I am using firebase power to download the content for the LinkedHashMap but I do not any solution for retrieving the Question key value of the LinkedHashMap named d

I have this code:

linkedHashMapForLessonContent = (intent.getSerializableExtra(tags?.LESSON_MAP_TAG_NAME) as WrapperSerializer<LinkedHashMap<String, Any>>).get() as LinkedHashMap<String, Any>

Which returns a LinkedHashMap with the keys: {0={...}, 1={...}, 2={...}, 3={...}, 4={...}...} Then I try to get the "0" value this way

var d = (linkedHashMapForLessonContent as LinkedHashMap<String, Any>)["0"]
Log.e("d", d.toString())

which returns

{Question=What is Gravity?, Content=[https://firebasestorage.googleapis.com/someimage0, https://firebasestorage.googleapis.com/someimage1, https://firebasestorage.googleapis.com/someimage2, https://firebasestorage.googleapis.com/someimage3], Type=Images, Answer=0}

So I want to return the value of the Question as the following

Log.e("hello world", something to get the question value)

That will return

'What is Gravity?'

I have tried multiple solutions and searched multiple things but no thing has the answer for my problem, I would be thankful if someone helps me out with this :) Thank you

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ElDeivis
  • 50
  • 6
  • There is a joke about getting the question in there somewhere. In Java I'd try `(String) d.get("Question")` I suppose? – Maarten Bodewes Jan 14 '20 at 01:11
  • So you recommend me to pass the code to java? – ElDeivis Jan 14 '20 at 01:17
  • No, but I presume you can do the same in Kotlin? – Maarten Bodewes Jan 14 '20 at 01:17
  • yeah I actually tried that in kotlin which translates to d["Question"] but it seems like the d LinkedHashMap is not set as a HashMap but as an any value so I am like no way – ElDeivis Jan 14 '20 at 01:19
  • LinkedHashMap implements the Map interface, which means it has a get() method, which means you can use square brackets just fine with it, as any other Map in Kotlin. If you're having an error with that please be specific about how it's not working the way you expect. – Doug Stevenson Jan 14 '20 at 01:22
  • I don't understand your chain of casts in the first line of code. What the heck is going on in there? Can you simplify that without casting so much? – Doug Stevenson Jan 14 '20 at 01:24
  • Shouldn't you use `var d = linkedHashMapForLessonContent["0"] as LinkedHashMap`? – Maarten Bodewes Jan 14 '20 at 01:27
  • @Maarten-reinstateMonica I get an error so I changed it to: ```var d = (linkedHashMapForLessonContent as LinkedHashMap)["0"]``` which returns a ```any``` value so it is not useful at all to get the ```Question``` key value – ElDeivis Jan 14 '20 at 01:31
  • What error were you getting? Because if you cast it before retrieval of the value, then of course you get `Any`. I think you have to backtrack some... Now if you cast to `LinkedHashMap>` in your current then that might work, but I would expect you'd get the same error you encountered initially\. – Maarten Bodewes Jan 14 '20 at 01:33
  • @DougStevenson Yeah, so pretty much I have a class that Serializes a ```LinkedHashMap``` when I pass it as an Intent Extra because when you pass it you are going to get a HashMap so it messes up the order and then the class I have called ```Wrapper Serializer``` 'maintains' it as a ```LinkedHashMap``` to not lose the order, I just put it so that you know where the ```d``` is coming from that you help me to set it as a ```LinkedHashMap``` and not as a ```any``` value as it is returned in the code I have – ElDeivis Jan 14 '20 at 01:36
  • @Maarten-reinstateMonica yeah so I cast it as ```var d: LinkedHashMap = (linkedHashMapForLessonContent as LinkedHashMap)["0"] as LinkedHashMap``` but I get an error – ElDeivis Jan 14 '20 at 01:37
  • error: ```Unable to start activity ComponentInfo{com.example.sense/com.example.sense.CourseContentActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.LinkedHashMap``` – ElDeivis Jan 14 '20 at 01:48

1 Answers1

1

You should convert d.toString() to a JSON then get "question" value:

JSONObject(d.toString())["question"].toString()
AliSh
  • 10,085
  • 5
  • 44
  • 76