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 :
- Click on item from recyclerview which opens a new activity (solved)
- From the new activity I want to get the current data from local db from the item I’ve clicked.
- 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
I solved the problem. Here is the code if anyone wonders:
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val customerSelected = customerList[position]
holder.clientName.text = customerSelected.name
holder.clientID.text = customerSelected.clientId
holder.clientPhone.text = customerSelected.phone
holder.clientAddress.text = customerSelected.visitAddress
holder.clientCard.setOnClickListener {
val intent = Intent(context, InfoActivity::class.java)
intent.putExtra("clientName", holder.clientName.text)
intent.putExtra("clientId", holder.clientID.text)
context.startActivity(intent)
}
}
This Question was asked in StackOverflow by Lagos77 and Answered by Lagos77 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.