0

This is the code for the activity where the rows will be shown

class doctors : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_doctors)

    val recyclerView = findViewById<RecyclerView>(R.id.docrecview)


    recyclerView.layoutManager = LinearLayoutManager(this)


    val docs = ArrayList<DocData>()





    val btn_click_me = findViewById<Button>(R.id.consult_btn)

    btn_click_me.setOnClickListener {
        Toast.makeText(this, "You clicked me.", Toast.LENGTH_SHORT).show()
    }


    val TAG = "My message"
    val db = FirebaseFirestore.getInstance()
//calling data from firebase to populate rows
    db.collection("doctors")
        .get()
        .addOnSuccessListener { result ->
            for (document in result) {
                Log.d(TAG, "${document.id} => ${document.data}")
                docs.add(DocData(document.data["name"].toString(), document.data["specialization"].toString()))

            }
            recyclerView.adapter = Adapter(docs)


        }
        .addOnFailureListener { exception ->
            Log.d(TAG, "Error getting documents: ", exception)
        }




}

And this is the adapter

class Adapter (val doctorlist: ArrayList<DocData>) : RecyclerView.Adapter<Adapter.ViewHolder>() {

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


}

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

override fun onBindViewHolder(holder: Adapter.ViewHolder, position: Int) {
    val doc:DocData = doctorlist[position]

    holder.txtname.text =  doc.name
    holder.txtSpec.text = doc.specialization

}

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

    val txtname = itemView.findViewById<TextView>(R.id.textName)
    val txtSpec = itemView.findViewById<TextView>(R.id.major)
    val btn = itemView.findViewById<Button>(R.id.consult_btn)

}

}

The red marked area is a button in a recyclerview and I want to go to another activity by clicking on that button and I am not able to figure it out this is my learning phase I will be grateful if you guys will help Thankyou in advance

Tameem
  • 155
  • 2
  • 11

2 Answers2

0

See https://stackoverflow.com/a/58836873/2914140. You need to create an interface like:

public interface ClickListener {
    void onClick(int position);
}

There you can put any parameters, not only position. In activity/fragment implement the interface. Then call it from onBindViewHolder():

@Override
public void onBindViewHolder(holder: Adapter.ViewHolder, position: Int) {
     holder.textView1.setOnClickListener{
         clickListener.onClick(position));
     }
}

Also you need to set clickListener with a method or constructor of the adapter.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • And what? It is very easy, a slight difference in this example. I also write in Kotlin (not in this case). – CoolMind Mar 17 '20 at 09:51
0

You have two options. Either use interfaces or higher order function, I prefer the later. with functions you pass an onClick function that you invoke when your btn is clicked:

class Adapter(
    private val docList: List<DocData>,
    private val onClick: (DocData) -> Unit // a function that takes a DocData parameter
) : RecyclerView.Adapter<Adapter.ViewHolder>() {
    // ......

    override fun onBindViewHolder(holder: Adapter.ViewHolder, position: Int) {
        val doc: DocData = doctorlist[position]
        btn.setOnClickListener {
            onClick.invoke(doc)
        }
        //.....
    }

}

then pass a function when instantiating an Adapter:

Adapter(docs) { docData ->
    // btn was clicked in this docData
}
Mohamed Mohsin
  • 940
  • 4
  • 10
  • Hi there Mohamed, why do we need to pass the function through the adapter, rather than just defining it in the onBindViewHolder – Liam Baron Mar 17 '21 at 22:21
  • You can't change the signature of `onBindVeiwHolder()` and you shouldn't call yourself . the owner of the `Adapter` object needs to pass the `onClick` function either with the constructor (like above) or with another setter function. – Mohamed Mohsin Mar 18 '21 at 13:22