[英]Unresolved Reference: context inside RecyclerView adapter class Kotlin
I am new in using classes in Kotlin. 我是在Kotlin中使用课程的新手。 How would I be able to solve this Unresolved references: context?
我将如何解决这个未解决的引用:上下文? I tried the same code on my MainActivity.kt and it works.
我在MainActivity.kt上尝试了相同的代码,并且可以正常工作。 What am I doing wrong here?
我在这里做错了什么?
class ListAdapter : RecyclerView.Adapter<ListAdapter.ViewHolder>() {
private val file = File(context.filesDir,"internalstoragefilename")
private val contents = file.readText()
}
Context is not available in Adapter by default. 默认情况下,适配器中没有上下文。 If you truly need it in this place, you could pass it as a constructor's parameter.
如果在这个地方确实需要它,则可以将其作为构造函数的参数传递。 Eg
例如
class ListAdapter(private val context: Context) : RecyclerView.Adapter<ListAdapter.ViewHolder>() {
private val file = File(context.filesDir,"internalstoragefilename")
private val contents = file.readText()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.