0

I have RecyclerView in my project and I am displaying all items using this RecyclerView. My question here is how to store the name that I clicked in my RecyclerView item. Code is working fine and any help will be appreciated.

Code

class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
    fun bindItem (test : TestModel) {
        var testName : TextView = itemView.findViewById(R.id.TestName)
        testName.text = test.TestName

        itemView.setOnClickListener { 
            val intent = Intent(itemView.context, Test:: class.java)
            itemView.context.startActivity(intent)
        }
    }
}
DawidJ
  • 1,245
  • 12
  • 19
  • `getAdapterPosition()` is what you are looking for . it will give you position of clicked item then you can get data from your dataset for this position . – ADM Dec 27 '18 at 03:58

2 Answers2

0

you can textView.setTag(xx),and getTag() in onClickListener

0

current form

class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
{

    fun bindItem(Test:TestModel)
    {
       var TestName:TextView=itemView.findViewById<TextView>(R.id.TestName)
        TestName.text=Test.TestName

        itemView.setOnClickListener {

            val intent= Intent(itemView.context, Test::class.java)
             intent.putExtra("name",TestName.Text.toString())
            itemView.context.startActivity(intent)
                                     }
    }
}

to the form want to go is

var testName:String=intent.getStringExtra("name")

or for java:

Intent intent = getIntent();
String name = intent.getExtraString("name")  
madlymad
  • 6,367
  • 6
  • 37
  • 68
jhayjhay
  • 78
  • 8
  • Thanks @jhayjhay for your code. I have modified the code as per kotlin syntax. Above code works for me. –  Dec 27 '18 at 15:14