I am implementing a RecylcerView.Adapter class in Kotlin. I am getting compile time error, see comments in 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 have checked it in Kotlin documentation, No solution.
Has anyone else faced this issue?
My bad, a silly mistake. I converted Java code to Kotlin in Android Studio, So it converted inner class as internal inner class.
I just removed internal it works fine.
I was going to delete this question, and just thought same issue someone might run into, so just posted an answer.
There are two aspect of this issue:
If you have outer class as public then Kotlin does not allow you to expose it's private or internal member as superclass argument, or as return type or even as a parameter. The inner class should have the same modifier as the eclosing class i.e. public.
Another aspect is if you have outer class as private or internal then in that case your inner class can be private or internal respectively.
In my case a message was: "'public' function exposes its 'internal' parameter type SomeType". Before it I have changed a class visiblity from internal
to:
class ClassName(type: SomeType, ...)
So, I found the class SomeType:
internal enum class SomeType
and removed internal
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With