Item from recyclerview to new activity and get current data from local db


This Content is from Stack Overflow. Question asked by Lagos77

I’m new at Kotlin and trying to solve this problem by googling and watching youtube. Can’t find anything specific for my problem, which are these :

  1. Click on item from recyclerview which opens a new activity (solved)
  2. From the new activity I want to get the current data from local db from the item I’ve clicked.
  3. I got a reference to use (clientId) and want to check if clientId on clicked item from recyclerview is the same from local db in the new activity.

This is the Activity that shows the recyclerview:

class ClientActivity : AppCompatActivity() {

private lateinit var binding: ClientActivityBinding

private lateinit var customerViewModel: CustomerViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    binding = ClientActivityBinding.inflate(layoutInflater)

    val recyclerView = binding.clientRecyclerview
    val adapter = ClientListAdapter()
    recyclerView.adapter = adapter
    val linearLayoutManager = LinearLayoutManager(this)
    linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
    recyclerView.layoutManager = linearLayoutManager

    customerViewModel = ViewModelProvider(this)[]
    customerViewModel.readAllData.observe(this, Observer { customer ->

    adapter.setOnItemClickListener(object : ClientListAdapter.onItemClickListener{
        override fun onItemClick(position: Int) {
            Log.d("testar", position.toString())
            val intent = Intent(this@ClientActivity,


My adapter :

class ClientListAdapter : RecyclerView.Adapter<ClientListAdapter.MyViewHolder>() {

private lateinit var clientListener : onItemClickListener

interface onItemClickListener {
    fun onItemClick(position: Int)

fun setOnItemClickListener(listener : onItemClickListener){
    clientListener = listener

private var customerList = emptyList<Customer>()

class MyViewHolder(itemView: View, listener: onItemClickListener) : RecyclerView.ViewHolder(itemView) {
    val clientName : TextView = itemView.findViewById(
    val clientID : TextView = itemView.findViewById(

    init {
        itemView.setOnClickListener {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.client_item, parent, false)

    return MyViewHolder(itemView,clientListener)

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val customerSelected = customerList[position]
    holder.clientName.text =
    holder.clientID.text = customerSelected.clientId

        val title =
        val id = customerSelected.clientId

fun setData(customer : List<Customer>){
    this.customerList = customer

override fun getItemCount(): Int {
    return customerList.size


New activity to check if clientId is the same from local db:

    class InfoActivity : AppCompatActivity() {
private lateinit var binding: ActivityInfoBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityInfoBinding.inflate(layoutInflater)

        val clientPosition = intent.getStringExtra("position")
        Log.d("WERA", clientPosition.toString())


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?