Access list from within a ViewHolder in Kotlin

0

I need to access a list that is in the Adapter, however, I need to access being in a ViewHolder, I know that with Java it would only refer to the list, but in Kotlin the list is not recognized,

class SearchFilterAdapter(private val filterList: List<Filter>) :
        RecyclerView.Adapter<SearchFilterAdapter.ViewHolder>(), FastScrollRecyclerView.SectionedAdapter {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_search_filter, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return filterList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(filterList[position])
    }

    override fun getSectionName(position: Int): String {
        return filterList[position].group?.toUpperCase()!!
    }

    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

        val sectionTextView = itemView?.sectionTextView
        val filterCheckBox = itemView?.filterCheckBox
        val rootLayout = itemView?.rootLayout

        fun bind(filter: Filter) {

            if(filter.isFirst) {
                sectionTextView?.visibility = View.VISIBLE
                sectionTextView?.text = filter.group?.toUpperCase()
            } else {
                sectionTextView?.visibility = View.GONE
            }

            filterCheckBox?.isChecked = filter.isChecked

            filterCheckBox?.setOnCheckedChangeListener {
                buttonView, isChecked ->
                //filterList[position].isChecked = isChecked
            }

            filterCheckBox?.text = filter.category
        }
    }
}

Note that there is a comment, that is where I tried to access the list that is in the adapter.

    
asked by anonymous 25.05.2018 / 21:38

1 answer

2

In Kotlin, a nested class , by default, can not access members of the class from outside.

To accomplish this, use the inner modifier in the nested class declaration:

class SearchFilterAdapter(private val filterList: List<Filter>) :
        RecyclerView.Adapter<SearchFilterAdapter.ViewHolder>(), FastScrollRecyclerView.SectionedAdapter {

    ...

    inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        ...
    }
}

You can read more details on nested and inner classes documentation .

    
05.06.2018 / 21:49