RecyclerView.Adapter- Error: public functions reveal their internal return type in Kotlin

I am implementing the RecylcerView.Adapter class in Kotlin. I get a compile-time error, see Comments in the following code.

// Compile time Error: 'public' function exposes its 'internal' return type ViewHolder
class DietListAdapter(context: Context, private val foodList: ArrayList<Food>) : RecyclerView.Adapter<DietListAdapter.ViewHolder>() {

    private val inflater: LayoutInflater
    private var onItemClick: Callback<Void, Int>? = null

    init {
        inflater = LayoutInflater.from(context)
    }
    // Compile time Error: 'public' function exposes its 'internal' return type ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DietListAdapter.ViewHolder {
        val holder = ViewHolder(inflater.inflate(R.layout.layout_food_list_item, parent, false))
        return holder
    }
    // Compile time Error: 'public' function exposes its 'internal' parameter type ViewHolder
    override fun onBindViewHolder(holder: DietListAdapter.ViewHolder, position: Int) {
        holder.textViewFoodName.text = foodList[position].foodName
        holder.textViewFoodDesc.text = foodList[position].foodDesc

        holder.itemView.setOnClickListener {
            if (onItemClick != null)
                onItemClick!!.callback(foodList[position].foodId)
        }
    }
    ...
    ...
    internal inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var textViewFoodName: TextView
        var textViewFoodDesc: TextView

        init {
            textViewFoodName = itemView.findViewById(R.id.textViewFoodName) as TextView
            textViewFoodDesc = itemView.findViewById(R.id.textViewFoodDesc) as TextView
        }
    }
    ...
    ...
}

I checked it in the Kotlin documentation, there is no solution.

Has anyone else encountered this problem?

+4
source share
3 answers

My bad, stupid mistake. I converted the Java code in Kotlin to Android Studio, so it converted the inner class as inner inner class.

I just deleted the internal one, it works fine.

I was going to remove this question, and just thought about the same problem that someone might run into, so I just posted the answer.

+14
source

:

  • , Kotlin , , . , eclosing class i.e. Public.

  • - , , .

+2

: "" public " " " SomeType". internal :

class ClassName(type: SomeType, ...)

, SomeType:

internal enum class SomeType

internal.

0

Source: https://habr.com/ru/post/1677672/


All Articles