Toast消息在Recycler View中不起作用

//此方法绑定列表上的数据

override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.bindItems(userList[position])
    holder.imgDelete.setOnClickListener(View.OnClickListener {
        Toast.makeText(this,"Delete Button Clicked", Toast.LENGTH_SHORT).show()

    })
    holder.imgCopy.setOnClickListener(View.OnClickListener {
        Toast.makeText(this,"Copy Button Clicked", Toast.LENGTH_SHORT).show()
    })
}

错误得到:-以下任何一个函数都不能用提供的参数调用: public open fun makeText(p0:Context !, p1:CharSequence !, p2:Int):吐司!在android.widget.Toast中定义

请检查并帮助

评论
xeos
xeos

采用

Toast.makeText(<Your Activity Context>,"Copy Button Clicked", Toast.LENGTH_SHORT).show()

代替

Toast.makeText(this,"Copy Button Clicked", Toast.LENGTH_SHORT).show()
点赞
评论
lillo
lillo

I think, the Application context, used here should be the activity class context, instead of this, because this inside an onClickListener, is actually not a applicationContext to the activity but that of the parent view.

尝试这个:

Toast.makeText(<Here_comes_your_activity_context>,"Copy Button Clicked", Toast.LENGTH_SHORT).show()

不要忘记用您自己的内容替换这里的活动上下文。

点赞
评论