简体   繁体   中英

Unable to reference sub class in Kotlin

As you can see below, I can't reference the inner class in my CustomAdapter for my app BindItems . I'm not sure why it is showing up as red and not found.

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


    }

Thanks -R

Change your onBindViewHolder method like below and also change the name of your ViewHolder :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM