0

I'm having an issue with passing a JSON object (my data class) as an argument to be shown as information in another fragment. I have a Data Class set up as @Serializable that should be passing ALL of its information through an argument to the next fragment.

The goal is to search an API for a phrase (working), get back the response in a RecyclerView (working), then set a clickListener to open a new fragment so the user can see all the values of the JSON object in the next view (not working).

My data class is set up as such:

@Serializable
@Entity("ud_table")
data class UdResponse(
    @PrimaryKey
    @SerialName("defid") val defId: Int,
    var definition: String,
    val permalink: String,
    val author: String,
    val word: String,
    val example: String,
    var isSaved: Boolean = false,
    @SerialName("written_on") val written: String,
    @SerialName("thumbs_up") val thumbsUp: Int,
    @SerialName("thumbs_down") val thumbsDown: Int,
)

The way I am holding this information is in the ViewModel like so:

private val _words = MutableLiveData<List<UdResponse>>()
    val words: LiveData<List<UdResponse>>
        get() = _words

I am sending the information to the next fragment using this function from the first fragment:

fun setupRecycler() {
        adapter = SearchFragmentAdapter(SearchFragmentAdapter.ClickListener {
            findNavController()
                .navigate(SearchFragmentDirections.actionSearchFragmentToWordDetail(it))
        })
        binding.searchFragmentRecycler.adapter = adapter
    }

which is calling this class and function in the adapter:

class ClickListener(val clickListener: (udResponse: UdResponse) -> Unit) {
        fun onClick(udResponse: UdResponse) {
            clickListener(udResponse)
        }
    }

Is my Data Class missing something I can't see? Am I implementing something incorrectly to make the nav_graph not recognize it as Serializable? Is this method of moving data around incorrect?

I'm still really new to Kotlin and I feel like I'm way in over my head.

0 Answers0