[英]Why does my code not enter this adapter class?
I'm new to Kotlin and Android studio.我是 Kotlin 和 Android 工作室的新手。 I'm trying to get a recycle view to work properly but I'm running into a problem when trying to use an adapter class.我试图让回收视图正常工作,但在尝试使用适配器类时遇到了问题。
I tried taking a look with breakpoints.我试着看看断点。 but it seems to trigger on the very first line where the class gets defined.但它似乎在定义类的第一行触发。 ( so class OrganisationsAdapter...etc ) and after that it skips the whole class, it doesnt even enter it. (所以 class OrganisationsAdapter...etc )然后它跳过整个课程,它甚至不输入它。
I also don't get any exceptions.我也没有任何例外。
Adapter Class适配器类
class OrganisationsAdapter(
private val myDataset: Array<String>
) : RecyclerView.Adapter<OrganisationsAdapter.MyViewHolder>() {
class MyViewHolder(
val textView: TextView
) : RecyclerView.ViewHolder(textView) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
var cell = LayoutInflater.from(parent.context).inflate(R.layout.example_item, parent, false)
val textView = cell.findViewById<TextView>(R.id.organisation_name)
return MyViewHolder(textView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = myDataset[position]
}
override fun getItemCount() = myDataset.size
}
**The line I use to call the class ** **我用来给班级打电话的线路**
recyclerViewOrganisationFragment.adapter = OrganisationsAdapter(Array(getTopics().size) { i -> getTopics()[i].name })
You need to pass the inflated layout to ViewHolder instead of TextView您需要将膨胀的布局传递给 ViewHolder 而不是 TextView
ex:前任:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
var cell = LayoutInflater.from(parent.context).inflate(R.layout.example_item, parent, false)
return MyViewHolder(cell)
}
class MyViewHolder(
val itemView: View
) : RecyclerView.ViewHolder(itemView) {
//TODO get textview here itemView.findViewByID
}
Update the viewholder accordingly with textview etc.,使用 textview 等相应地更新视图,
Thank you Lakhwinder.谢谢拉赫温德。
"You need to pass on ArrayList on the constructor of the adapter, you are passing a size" “您需要在适配器的构造函数上传递 ArrayList,您正在传递一个大小”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.