简体   繁体   English

为什么我的代码没有进入这个适配器类?

[英]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.

相关问题 为什么我的recyclerview适配器不起作用? /为什么班级没有上课? - Why does my recyclerview adapter not work? / Why is the class not running? 如果之前的代码没有解锁,为什么我的代码进入了锁定区域? - Why does my code enter a locked area if the previous code has not released the lock? 为什么我的 RecyclerView 适配器不起作用? - Why My RecyclerView adapter does not work? 为什么将RecyclerView适配器包装在另一个类中会导致不同的行为? - Why does wrapping my RecyclerView adapter in another class cause different behaviour? 为什么重置我的适配器不使用新的ArrayList? - Why does resetting my Adapter not take a new ArrayList? 为什么我的 Android Adapter 没有 getActivity() 方法 - Why does my Android Adapter not have the getActivity() method 为什么我的Android自定义适配器样式随机行? - Why does my Android custom adapter style random rows? 为什么我的代码不起作用? - Why does my code not work? 为什么`parcelize` 不会在我的自定义回收器适配器类和我的自定义活动类之间传递数据? - Why won't `parcelize` pass data between my custom recycler adapter class and my custom activity class? 为什么我的应用程序会因 NullPointerException 而崩溃,为什么我的适配器(可能)为空? - Why does my app crash with a NullPointerException, and why is my adapter (probably) null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM