我是Kotlin的新手,我从教程中尝试了RecycleView的实现,但是得到了
class MainAdapter: RecyclerView.Adapter<CustomViewHolder>() {
val videoTitles = listOf("First Title","Second","Third")
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.video_row,parent,false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder?, position: Int) {
val videoTitle = videoTitles.get(position)
holder.view.textView_video_title.text = videoTitle
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
}
我正在使用Android Studio 3.4,是否会导致错误?
答案 0 :(得分:2)
我已经在评论中回答了。为了结束问题,我将其发布为答案。
问题:-onBindViewHolder
参数在父类中不能为空,因此,当您覆盖它时,它应该是相同的。
class MainAdapter: RecyclerView.Adapter<MainAdapter.CustomViewHolder>() {
val videoTitles = listOf("First Title","Second","Third")
override fun getItemCount(): Int {
return videoTitles.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.activity_dx,parent,false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
}
inner class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view){
}
}
答案 1 :(得分:1)
class MyOrderRecycleAdapter(val cont: Context, val ordersList: List<MyOrderModel.Datum>) : RecyclerView.Adapter<MyOrderRecycleAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyOrderRecycleAdapter.ViewHolder {
val viewHoler = LayoutInflater.from(cont).inflate(R.layout.my_orders_rc_row, parent, false)
return ViewHolder(viewHoler)
}
override fun getItemCount(): Int {
return ordersList.size
}
override fun onBindViewHolder(holder: MyOrderRecycleAdapter.ViewHolder, position: Int) {
Glide.with(cont).load(ordersList!!.get(position)!!.product_image).into(holder.product_icon);
holder.product_name.text = ordersList!!.get(position)!!.name
}
inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
val product_icon: ImageView = itemView!!.findViewById(R.id.product_icon);
val product_name: TextView = itemView!!.findViewById(R.id.product_name);
}
}