Search code examples
javaandroid-studiokotlinandroid-recyclerviewrealm

How to make Recyclerview items clickable?


I am trying to make a notes app which will allows the user make simple notes and save it. I used realm database to save the users notes. The notes are showing up fine in the recyclerview but I cannot click them.

What I want to happen is, When one of the notes in the recyclerview is clicked I want to start a new acitivty.

adapter

class NotesAdapter(private val context:Context, private val notesList: RealmResults<Notes>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){

   private lateinit var mOnClickListener:View.OnClickListener
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.notes_rv_layout,parent,false)
        v.setOnClickListener(mOnClickListener)
        return ViewHolder(v)
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder.itemView.titleTV.text = notesList[position]!!.title
        holder.itemView.descTV.text = notesList[position]!!.description
        holder.itemView.idTV.text = notesList[position]!!.id.toString()

    }
    private fun onClick(view:View) {
        
    }

    class ViewHolder(v: View?):RecyclerView.ViewHolder(v!!){
        val title = itemView.findViewById<TextView>(R.id.titleTV)
        val desc = itemView.findViewById<TextView>(R.id.descTV)
        val id = itemView.findViewById<TextView>(R.id.idTV)

    }
}

main activity

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

        //init views
        realm  = Realm.getDefaultInstance()
        addNotes = findViewById(R.id.addNotes)
        notesRV = findViewById(R.id.NotesRV)

        //onclick add notes button

        addNotes.setOnClickListener {
            val intent = Intent(this, AddNotesActivity::class.java)
            startActivity(intent)
            finish()
        }

        notesRV.layoutManager = StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL)
        getAllNotes()
    }

    private fun getAllNotes() {

        notesList = ArrayList()
        val results:RealmResults<Notes> = realm.where<Notes>(Notes::class.java).findAll()
        notesRV.adapter = NotesAdapter(this, results)
        notesRV.adapter!!.notifyDataSetChanged()
    }
}

Solution

  • use onBindViewHolder for that:

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    
        holder.itemView.titleTV.text = notesList[position]!!.title
        holder.itemView.descTV.text = notesList[position]!!.description
        holder.itemView.idTV.text = notesList[position]!!.id.toString()
    
        holder.itemView.setOnClickListener{
           val intent = Intent(holder.itemView.context,YourActivity::class.java)
           holder.itemView.context.startActivity(intent)
        }
    
    }