Home > front end >  problem, can't create an event that generates a toast message on kotlin recyclerview
problem, can't create an event that generates a toast message on kotlin recyclerview

Time:08-13

I'm using Kotlin to create an event that generates a toast message on click of a recyclerview. I run into trouble making a Tost message in a recyclerview event. I tried the following page, but couldn't solve it. Toast message is not working in Recycler View

error code is

in kotlin & 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

PrintActivity.kt

package com.questionbank

class PrintActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val vBinding = ActivityPrintBinding.inflate(layoutInflater)
        setContentView(vBinding.root)
        val helper = SqliteHelper(this, "myDB.sql", 1)

        var recyclerViewAdapter = CustomAdapter()
        recyclerViewAdapter.listData = helper.select()
        vBinding.myRecyclerView.adapter = recyclerViewAdapter
        vBinding.myRecyclerView.layoutManager = LinearLayoutManager(this)
        vBinding.myRecyclerView.addItemDecoration(
            DividerItemDecoration(this, DividerItemDecoration.VERTICAL)

        )

    }

    class CustomAdapter : RecyclerView.Adapter<CustomAdapter.Holder>() {
        var listData = ArrayList<questionType>()

        inner class Holder(val vBinding: QuestionLayoutRecyclerBinding) :
            RecyclerView.ViewHolder(vBinding.root) {
            fun setData(id:Int?, question: String, answer: String, exp: String) {
                vBinding.printId.text=id.toString()

                
                vBinding.myLinear.setOnClickListener {
                    // error occur
                    Toast.makeText(this@PrintActivity, "test", Toast.LENGTH_SHORT).show()
                }

            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            val vBinding = QuestionLayoutRecyclerBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            return Holder(vBinding)
        }

        override fun onBindViewHolder(holder: Holder, position: Int) {
            val question = listData[position]
            holder.setData(question.id, question.question, question.answer, question.exp)
        }

        override fun getItemCount(): Int {
            return listData.size
        }

    }

}

activity_print

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PrintActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/myRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

question_layout_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/myLinear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@ id/printId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

CodePudding user response:

Two ways to fix,

  1. Make CustomAdapter class as inner class.

    innner class CustomAdapter : RecyclerView.Adapter<CustomAdapter.Holder>() {
    

So toast function it will take constant from activity class.

  1. In viewholder, get context from view. it.context will get context from linearlayout.

    vBinding.myLinear.setOnClickListener {
                     Toast.makeText(it.context, "test", Toast.LENGTH_SHORT).show()
                 }
    

Its recommended to place adapter logic in separate file and use second solution. So you dont need to make adapter as inner class.

  • Related