无法在Kotlin中引用子类

时间:2017-09-29 12:10:04

标签: java android class kotlin

如下所示,我无法在CustomAdapter中为我的应用BindItems引用内部课程。我不确定为什么它会显示为红色而未找到。

 class MyCustomAdapter(context: Context,val theList: ArrayList<MyInfo>): RecyclerView.Adapter<ViewHolder>(){

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

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            holder.bindItems(theList[position])
        }

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

        class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
              fun bindItems(user: MyInfo) {
                 val textViewName : TextView = itemView.findViewById<TextView>(R.id.row_town_name)
                 val textViewAddress : TextView  = itemView.findViewById<TextView>(R.id.row_zip_code)
                 textViewName.text = user.townname
                 textViewAddress.text = user.zipcode
             }
         }       
    }

SOLUTION:

 class MyCustomAdapter(context: Context,val theList: ArrayList<MyInfo>): RecyclerView.Adapter<MyCustomAdapter.MyViewHolder>(){


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


         override fun onBindViewHolder(holder: MyCustomAdapter.MyViewHolder, position: Int) {
            holder.bindItems(theList[position])
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyCustomAdapter.MyViewHolder {
            val v = LayoutInflater.from(parent.context).inflate(R.layout.main_row, parent, false)
            return MyViewHolder(v)
        }

        class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
              fun bindItems(user: MyInfo) {
                 val textViewName : TextView = itemView.findViewById<TextView>(R.id.row_town_name)
                 val textViewAddress : TextView  = itemView.findViewById<TextView>(R.id.row_zip_code)
                 textViewName.text = user.townname
                 textViewAddress.text = user.zipcode
             }
         }


    }

由于 -R

1 个答案:

答案 0 :(得分:-1)

更改您的onBindViewHolder方法,如下所示,同时更改ViewHolder的名称:

override fun onBindViewHolder(holder: MyViewHolder, position: Int)
{
}