Home > Back-end >  How to add a RecyclerView in a fragment Kotlin- I initialize the dataset but idk what to to with con
How to add a RecyclerView in a fragment Kotlin- I initialize the dataset but idk what to to with con

Time:07-15

ItemAdapter:

class ItemAdapter( val context: Context, val dataset: List) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {

Fragment:

val dataset: List =ArrayList() val context: Context =

//so i can initialize the-> adapter= ItemAdapter(context, dataset)

CodePudding user response:

You can get a context in fragment using requireContext(). It will be a context of Activity running current fragment

https://developer.android.com/reference/androidx/fragment/app/Fragment#requireContext()

CodePudding user response:

You don't need a context, if you need it later you can get it from some view.

If required, you can use requireContext(), but if you remove the fragment when it's creating the adapter, the app will crash, because you'd be requiring a context you don't have.

  • Related