Home > Blockchain >  how to make the items of my Firebase Recycler adapter onClick with kotlin android
how to make the items of my Firebase Recycler adapter onClick with kotlin android

Time:12-30

with the help of FirestoreUI I established a recyclerview that contains items from my cloud firestore database in it . And now I would like to be able to trigger an event* after clicking on an item of this list ( *the event is basically a new activity where I can find more details about the item) I am using Kotlin and android studio . this is the activity where I put all the code I needed :

    class UserViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
class MeetingActivity : AppCompatActivity() {
    val db = Firebase.firestore
    private val newMeetingActivityRequestCode = 1
    private lateinit var rvMeetings : RecyclerView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_meeting)
        rvMeetings = findViewById(R.id.rvMeetings)
        val query : CollectionReference = db.collection("Meetings")
        val options = FirestoreRecyclerOptions.Builder<Meeting>().setQuery(query , Meeting::class.java)
            .setLifecycleOwner(this).build()
        val adapter = object: FirestoreRecyclerAdapter<Meeting, UserViewHolder>(options){
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.meeting_raw_layout, parent, false)
                return UserViewHolder(view)
            }
                override fun onBindViewHolder(holder: UserViewHolder, position: Int, model: Meeting) {
                val tvName : TextView = holder.itemView.findViewById(R.id.tv1)
                val tvEmojis : TextView = holder.itemView.findViewById(R.id.tv2)
                tvName.text = model.name
                tvEmojis.text = model.time
            }
        }
        rvMeetings.adapter = adapter
        rvMeetings.layoutManager = LinearLayoutManager(this)

        val fab: View = findViewById(R.id.fab)
        fab.setOnClickListener {
            fabOnClick()
        }

    }

    private fun fabOnClick() {
        val intent = Intent(this, AddMeetingActivity::class.java)
        startActivityForResult(intent, newMeetingActivityRequestCode)
    }

}

any help would be really appreciated and thanks !

CodePudding user response:

setOnClickListener in your list item

  override fun onBindViewHolder(holder: UserViewHolder, position: Int, model: Meeting) {
                val tvName : TextView = holder.itemView.findViewById(R.id.tv1)
                val tvEmojis : TextView = holder.itemView.findViewById(R.id.tv2)
                tvName.text = model.name
                tvEmojis.text = model.time
                holder.itemView.setOnClickListener {
                    //do something
                }
  • Related