To make ViewHolder treat each radio button separately and not reuse previous radio button's state I stored the state of whether or not a radio button item is checked in a list of booleans and set the radio buttons state according to that list. but when I scroll down and up the toggled radio buttons get untoggled randomly even though the isCheckedList contains the right boolean information for each radio button(I print the list with logd() to verify if it actually stores the states)
Any Idea of what I'm missing?
class QuestionAdapter(context: Context) :
RecyclerView.Adapter<QuestionAdapter.QuestionViewHolder>() {
private val listOfQuestion: List<String>
private val listSize = 20
private val isCheckedList: MutableList<Boolean> = MutableList(listSize) { false }
//Initialize a list of 20 questions.
init {
val allQuestions = context.resources.getStringArray(R.array.questions).toList()
listOfQuestion = allQuestions
.shuffled()
.take(listSize)
}
class QuestionViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val questionText = view.findViewById<TextView>(R.id.question)
val radioButton = view.findViewById<RadioButton>(R.id.yes)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionViewHolder {
val layout = LayoutInflater
.from(parent.context)
.inflate(R.layout.item_view, parent, false)
return QuestionViewHolder(layout)
}
override fun onBindViewHolder(holder: QuestionViewHolder, position: Int) {
val item = listOfQuestion[position]
holder.questionText.text = item
holder.radioButton.isChecked = isCheckedList[position]
holder.radioButton.setOnCheckedChangeListener { _, isChecked ->
isCheckedList[holder.adapterPosition] = isChecked
Log.d("Adapter", "list: $isCheckedList ")
}
}
override fun getItemCount(): Int {
return listOfQuestion.size
}
}