在 Kotlin Adapter 中获取 getItemCount 的上下文

发布于 2025-01-13 23:55:00 字数 1936 浏览 2 评论 0原文

我正在寻找一种方法来查找 viewholder 方法之外的 RecyclerView 适配器的上下文。我有两个不同的数组,我希望能够根据所显示的活动/片段来显示它们(最终的应用程序将有 4 个数组,这就是为什么我不想为每个数组创建另一个适配器)。我可以使用holder.itemview.context让它在onbindviewholder中工作,但由于数组的大小不同,我也需要一种在getitemcount()中使用if语句的上下文的方法。在构造函数中传递上下文会导致“没有为参数传递任何值”。想知道是否有人使用片段或活动找到了此问题的解决方案。最坏的情况我只是为每个阵列制作不同的适配器

class ItemAdapter(var context: Context) : RecyclerView.Adapter<ItemAdapter.ViewHolder>() {

    private var weeklyTasks = arrayOf(R.string.app_name, R.string.daily_hint_1, R.string.daily_hint_2, R.string.daily_hint_3)
    private var dailyTasks = arrayOf(R.string.daily_hint_1, R.string.daily_hint_2, R.string.daily_hint_3)
    private var xp = intArrayOf(R.mipmap.tenxp_round, R.mipmap.fiftyxp_round, R.mipmap.hundredxp_round)

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

    override fun getItemCount(): Int {
        //cant use context here because no value is passed for parameter
        return dailyTasks.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //var context = holder.itemView.context -> this works here but not as a constructor
        if(context is ToDoList) {
            holder.itemImage.setImageResource(xp[0])
            holder.itemDetail.text = dailyTasks[position].toString()
        }
        else{
            holder.itemImage.setImageResource(xp[1])
            holder.itemDetail.text = weeklyTasks[position].toString()
        }
    }


    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        var itemImage: ImageView
        var itemDetail: TextView

        init {
            itemImage = itemView.findViewById(R.id.item_image)
            itemDetail = itemView.findViewById(R.id.item_detail)
        }

    }
}

I'm looking for a way to find the context of a RecyclerView Adapter outside of the viewholder methods. I have two different arrays that I would like to be able to display based on the activity/fragment that is being shown(the final app would have 4 arrays which is why I don't just want to make another adapter for each array). I can get it to work in the onbindviewholder using holder.itemview.context but since the arrays aren't the same size I need a way to use context for an if statement in getitemcount() as well. Passing context in the constructor results in "no value passed for parameter". Was wondering if anyone has found a solution to this problem using either fragments or activities. Worst case scenario I just make a different adapter for every array

class ItemAdapter(var context: Context) : RecyclerView.Adapter<ItemAdapter.ViewHolder>() {

    private var weeklyTasks = arrayOf(R.string.app_name, R.string.daily_hint_1, R.string.daily_hint_2, R.string.daily_hint_3)
    private var dailyTasks = arrayOf(R.string.daily_hint_1, R.string.daily_hint_2, R.string.daily_hint_3)
    private var xp = intArrayOf(R.mipmap.tenxp_round, R.mipmap.fiftyxp_round, R.mipmap.hundredxp_round)

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

    override fun getItemCount(): Int {
        //cant use context here because no value is passed for parameter
        return dailyTasks.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //var context = holder.itemView.context -> this works here but not as a constructor
        if(context is ToDoList) {
            holder.itemImage.setImageResource(xp[0])
            holder.itemDetail.text = dailyTasks[position].toString()
        }
        else{
            holder.itemImage.setImageResource(xp[1])
            holder.itemDetail.text = weeklyTasks[position].toString()
        }
    }


    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        var itemImage: ImageView
        var itemDetail: TextView

        init {
            itemImage = itemView.findViewById(R.id.item_image)
            itemDetail = itemView.findViewById(R.id.item_detail)
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文