Home > Net >  Kotlin: how to extract value of selectedItem of snipper?
Kotlin: how to extract value of selectedItem of snipper?

Time:01-02

I create Mood data class which has image and description.

data class Mood(val image: Int, val description: String)

And I make snipperAdapter and link it to snipper widget.

 binding.todayMood.adapter = activity?.applicationContext?.let {
            MoodArrayAdapter(
                it,
                listOf(
                    Mood(R.drawable.ic_emotion_1, "많이 슬퍼요"),
                    Mood(R.drawable.ic_emotion_2, "슬퍼요"),
                    Mood(R.drawable.ic_emotion_3, "평범해요"),
                    Mood(R.drawable.ic_emotion_4, "좋아요"),
                    Mood(R.drawable.ic_emotion_5, "많이 좋아요"),
                )
            )
        }

I want to send this selectedItem value of Snipper to DB.

Not both image and description, but only description.

But when I get this data from snipper, I can only use selectedItem,

binding.todayMood.selectedItem.toString()

this result comes as Mood(image=123, description="hi")

I can't access item of Mood like binding.todayMood.selectedItem.description

.description is not possible.

How can I only extract description from here?

CodePudding user response:

Cast selectedItem to the Mood type:

(binding.todayMood.selectedItem as Mood).description
  • Related