Android 外部类对象未从Kotlin中的内部类更新

Android 外部类对象未从Kotlin中的内部类更新,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我正在使用一个带有图像按钮的recyclerview。在适配器方法中,onBindViewHolder(..)。我捕获用户是否选择了图像按钮。在外部类中,无论用户是否选择了某个对象,我都会保留一个引用 我能够捕获用户选择,但外部类字段不会更新。我使用的是Kotlin,这里是一个例子 class Quiz : ConstraintLayout { constructor(context: Context?) : super(context) // This is updated in the a

我正在使用一个带有图像按钮的recyclerview。在适配器方法中,
onBindViewHolder(..)
。我捕获用户是否选择了图像按钮。在外部类中,无论用户是否选择了某个对象,我都会保留一个引用

我能够捕获用户选择,但外部类字段不会更新。我使用的是Kotlin,这里是一个例子

class Quiz : ConstraintLayout {
constructor(context: Context?) : super(context)

// This is updated in the apdater.
private var selectedOption : String? = null

@SuppressLint("ClickableViewAccessibility")
private fun inflate(context: Context) {
    LayoutInflater.from(context)
        .inflate(R.layout.image_layout, this, true) as ConstraintLayout

        viewAnimation.startTimerAnimation(7000) {
        // This always remain null. This callback is triggered after 7000 ms i.e. when animation completes.
            if (selectedOption == null)
                // do something less
            else // do something more
        }

}

inner class ImageAdapter() : RecyclerView.Adapter<ViewHolder>() {
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        optionButton.setOnClickListener {
        // I have put a log statement here and tested the selection is captured. I also see in on the screen.
            selectedOption = imageButtonMap[holder.optionButton].toString()

        }
    }
}
课堂小测验:ConstraintLayout{
构造函数(上下文:上下文?):超级(上下文)
//这将在apdater中更新。
私有变量selectedOption:字符串?=null
@SuppressLint(“ClickableViewAccessibility”)
私人娱乐膨胀(上下文:上下文){
LayoutFlater.from(上下文)
.充气(R.layout.image_layout,this,true)作为约束
viewAnimation.startTimerAnimation(7000){
//此值始终保持为空。此回调在7000毫秒后即动画完成时触发。
如果(selectedOption==null)
//少做点什么
要不然就多做点什么
}
}
内部类ImageAdapter():RecyclerView.Adapter(){
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
optionButton.setOnClickListener{
//我在这里放了一个日志语句,并测试了选择是否被捕获。我也在屏幕上看到了。
selectedOption=imageButtonMap[holder.optionButton].toString()
}
}
}
请让我知道,如果有更多的细节需要在这里。
作为测试,我还创建了属性lateinit,Kotlin抛出一个异常,表示lateinit属性未初始化。

尝试在onBindViewHolder中打印值并调用适配器notifyDataSetChanged()@RahulKhurana不起作用。类外的值仍然没有更新。还有其他解决方案吗?它对m起作用。我所做的是更改了内部类的值并调用notifyDataSetChanged()然后使用Logcat打印值。然后更改值。发布完整代码以找出不适用于您的案例的点尝试在onBindViewHolder中打印值并调用适配器notifyDataSetChanged()@RahulKhurana不起作用。类外的值仍然没有更新。还有其他解决方案吗?它对m起作用。我所做的是,我从内部类更改了值,并调用notifyDataSetChanged(),然后使用Logcat打印值。然后值被更改。发布完整代码以找出不适用于您的案例的点