Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "None of the following functions can be called with the arguments supplied:" with Toast

I want to create a code to click on items of RecyclerView. I found one from Internet, however it keep getting this error:

None of the following functions can be called with the arguments supplied:

public open fun makeText(p0: Context!, p1: CharSequence!, p2: Int): Toast! defined in android.widget.Toast

public open fun makeText(p0: Context!, p1: Int, p2: Int): Toast! defined in android.widget.Toast

Here's my code:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        val users = ArrayList<User>()

        val adapter = CustomAdapter(users)

        recyclerView.adapter = adapter

        recyclerView.addOnItemClickListener(object : OnItemClickListener {
            override fun onItemClicked(position: Int, view: View) {
                Toast.makeText(this, "Clicked on  " + users.get(position).name, Toast.LENGTH_LONG).show()
            }
        })


    }

    interface OnItemClickListener {
        fun onItemClicked(position: Int, view: View)
    }

    fun RecyclerView.addOnItemClickListener(onClickListener: OnItemClickListener) {
        this.addOnChildAttachStateChangeListener(object : RecyclerView.OnChildAttachStateChangeListener {
            override fun onChildViewDetachedFromWindow(view: View) {
                view.setOnClickListener(null)
            }

            override fun onChildViewAttachedToWindow(view: View) {
                view.setOnClickListener {
                    val holder = getChildViewHolder(view)
                    onClickListener.onItemClicked(holder.adapterPosition, view)
                }
            }
        })
    }

How can I fix that error message?

like image 329
FY Gamer Avatar asked Feb 04 '23 17:02

FY Gamer


1 Answers

Toast.makeText(this@YOUR_ACTIVITY_NAME, "Clicked on  " + users.get(position).name, Toast.LENGTH_LONG).show()
like image 158
Amir Abbas Avatar answered Feb 06 '23 16:02

Amir Abbas