I manage to display the items stored in a room database in a recycler view with checkboxes and i want to store the checked items in a list, to store the checked items on a list I use setOnClickListener on the checkbox like the code below in adapter but the application stop when i click to display the list or even if the list displayed with success sometimes it stops when i click on the checkbox of an item (for information when i remove the listener the list is displayed well and I can click on the checkboxes and everything works well but the problem is when i add the listener to store the checked items).
class Adapter (val selectedFluxs : MutableList<Flux>) : RecyclerView.Adapter<Adapter.VH>(
) {
class VH(itemView: View) : RecyclerView.ViewHolder(itemView){
lateinit var feed : Flux
}
var allFluxs : List<Flux> = listOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val v = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false)
val holder = VH( v )
v.check.setOnClickListener( ) {
// it as CheckBox
if( v.check.isChecked ){
selectedFluxs.add ( holder.feed )
}else{
selectedFluxs.remove( holder.feed )
}
}
return holder
}
override fun getItemCount(): Int {
return allFluxs.size
}
fun setFlux( allFlux : List<Flux> ) {
this.allFluxs = allFlux
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: VH, position: Int) {
holder.itemView.apply {
Source.text = allFluxs[position].source
Tag.text = allFluxs[position].tag
Url.text = allFluxs[position].adr
check.isChecked =
holder.feed in selectedFluxs
}
}
}