草庐IT

android - RecyclerView.Adapter - 错误 : public functions exposes its internal return type in Kotlin

coder 2023-05-08 原文

我正在 Kotlin 中实现一个 RecylcerView.Adapter 类。 我收到编译时错误,请参阅以下代码中的注释。

// 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
        }
    }
    ...
    ...
}

我在 Kotlin 文档中检查过,没有解决方案。

还有其他人遇到过这个问题吗?

最佳答案

我的错,一个愚蠢的错误。我在 Android Studio 中将 Java 代码转换为 Kotlin,因此将内部类转换为内部内部类。

我刚刚删除了内部它工作正常。

我打算删除这个问题,只是想有人可能会遇到同样的问题,所以只是发布了一个答案。

关于android - RecyclerView.Adapter - 错误 : public functions exposes its internal return type in Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44107376/

有关android - RecyclerView.Adapter - 错误 : public functions exposes its internal return type in Kotlin的更多相关文章

随机推荐