I have an annoying problem I wanted to use custom dialog in recycler view adapter but I need a context for builder and I wrote that but I can't initialized that. I looked up Internet but I can't find anything. Is anyone can help me? Thank you and have a good codes :)
private lateinit var context: Context
CodePudding user response:
1- From here you can pass Context like i am sending myContext From Activity/Faragment
binding.rvIssues.layoutManager =
LinearLayoutManager(myContext, RecyclerView.VERTICAL, false)
val adapterIssue = AdapterIssues(myContext, messagesList)
binding.rvIssues.adapter = adapterIssue
2 - In Adapter you can put keyWord var to globalise the scope of context and can be use any where in Adapter
class AdapterIssues(
var context: Context,
var messagesList: ArrayList<ResourceModel>,
var listener: SetOnIssueItemClickListener) :
RecyclerView.Adapter<AdapterIssues.ViewHolder>() {}
3- you don't need to put in adapter like
private lateinit var context: Context
CodePudding user response:
It would be great if you provide some of your code but, You could pass the context via your adapter constructor, something like this.
In Adapter you could write:
class MyAdapter(private val context: Context) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
private lateinit var mContext: Context
fun doSomething() {
mContext = context
}
}
In your Activity/Fragment:
private fun bindAdapter() {
val adapter = MyAdapter(context = this) // Use requireActivity() for fragments
}
Hope this help!!!