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

Issue

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?) {
    super.onCreate(savedInstanceState)
    binding = ClientActivityBinding.inflate(layoutInflater)
    setContentView(binding.root)

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


    //UserViewModel
    customerViewModel = ViewModelProvider(this)[CustomerViewModel::class.java]
    customerViewModel.readAllData.observe(this, Observer { customer ->
        adapter.setData(customer)
    })

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

}

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(R.id.clientTitle)
    val clientID : TextView = itemView.findViewById(R.id.clientId)

    init {
        itemView.setOnClickListener {
            listener.onItemClick(adapterPosition)
        }
    }
}

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 = customerSelected.name
    holder.clientID.text = customerSelected.clientId

    holder.itemView.setOnClickListener{
        val title = customerSelected.name
        val id = customerSelected.clientId
    }
}

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

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?) {
        super.onCreate(savedInstanceState)
        binding = ActivityInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)

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



Solution

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?