Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView.Adapter- Error: public functions exposes its internal return type in Kotlin

Tags:

android

kotlin

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?

like image 943
chandil03 Avatar asked May 22 '17 07:05

chandil03


3 Answers

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.

like image 186
chandil03 Avatar answered Oct 11 '22 16:10

chandil03


There are two aspect of this issue:

  1. 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.

  2. 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.

like image 39
Sriyank Siddhartha Avatar answered Oct 11 '22 16:10

Sriyank Siddhartha


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.

like image 23
CoolMind Avatar answered Oct 11 '22 18:10

CoolMind