what I want to achieve is to send data from custom recycle view adapter to activity when button is clicked. More specifically I want to send over an integer value.
RecycleAdapter code
class RecyclerAdapter(ingredientsList: MutableList<String>) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()
{
private var ingredients : MutableList<String> = ingredientsList
private var testInt : Int = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.lv_view_item,parent,false)
return ViewHolder(v,)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.pName.text = ingredients[position]
var ingrCount : TextView = holder.itemView.findViewById(R.id.ingredientCount)
var decrementIngr : Button = holder.itemView.findViewById(R.id.deleteIngeredient)
var incrementIngr : Button = holder.itemView.findViewById(R.id.addIngredient)
incrementIngr.setOnClickListener{
val count = Integer.parseInt(ingrCount.text.toString()) 1
ingrCount.text = "$count"
testInt = count
Log.w("w", testInt.toString())
}
decrementIngr.setOnClickListener{
val count = Integer.parseInt(ingrCount.text.toString()) - 1
ingrCount.text = "$count"
testInt = count
Log.w("w", testInt.toString())
}
}
override fun getItemCount(): Int {
return ingredients.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
{
var pName : TextView = itemView.findViewById(R.id.ingredientName)
init {
itemView.setOnClickListener{
val position : Int = adapterPosition
}
}
}
}
And in my activity I want to get testInt variable when I click a button.
btn_create_order.setOnClickListener{
//somehow get the data
}
Thanks
CodePudding user response:
First of all create an interface like OnItemClickValue
:
interface OnItemClickValue {
fun onValueChange(value: Int)
}
and pass through Adapter and call onValueChange()
method from onClick:
class RecyclerAdapter(ingredientsList: MutableList<String>, val onItemClickValue:OnItemClickValue) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()
{
private var ingredients : MutableList<String> = ingredientsList
private var testInt : Int = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.lv_view_item,parent,false)
return ViewHolder(v,)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.pName.text = ingredients[position]
var ingrCount : TextView = holder.itemView.findViewById(R.id.ingredientCount)
var decrementIngr : Button = holder.itemView.findViewById(R.id.deleteIngeredient)
var incrementIngr : Button = holder.itemView.findViewById(R.id.addIngredient)
incrementIngr.setOnClickListener{
val count = Integer.parseInt(ingrCount.text.toString()) 1
ingrCount.text = "$count"
testInt = count
onItemClickValue.onValueChange(testInt)
Log.w("w", testInt.toString())
}
decrementIngr.setOnClickListener{
val count = Integer.parseInt(ingrCount.text.toString()) - 1
ingrCount.text = "$count"
testInt = count
onItemClickValue.onValueChange(testInt)
Log.w("w", testInt.toString())
}
}
override fun getItemCount(): Int {
return ingredients.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
{
var pName : TextView = itemView.findViewById(R.id.ingredientName)
init {
itemView.setOnClickListener{
val position : Int = adapterPosition
}
}
}
}
Implement OnItemClickValue
interface on Activity:
class MainActivity: AppCompatActivity(), OnItemClickValue{
var testInt:Int = 0
override fun onValueChange(value: Int) {
testInt = value
}
btn_create_order.setOnClickListener{
//You will get testInt from here
Log.d("TAG", "testInt $testInt")
}
}