如何通过捆绑包将数据从适配器传递到Kotlin的片段?
我一直在尝试传递我的数据(用户的电子邮件和电话) 适配我的片段。从我在线阅读的内容,我应该使用 接口的接口,但我不能使用捆绑包。任何人都可以 在步骤中解释如何通过捆绑包传递数据以及如何收到数据 它在碎片中。以下是我的适配器。
class ProductAdapter(
private val onItemClick: (item: ProductResultData) -> Unit,
private val items: List<ProductResultData>
): RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
lateinit var context: Context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
context = parent.context
val binding: ProductItemDataBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.rv_product _item,
parent,
false)
return ProductViewHolder(binding)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val productItem = items[position]
holder.bindItem(productItem,context)
}
override fun getItemCount(): Int {
return items.size
}
inner class ProductViewHolder(private val binding: ProductItemDataBinding): RecyclerView.ViewHolder(binding.root){
@SuppressLint("SetTextI18n")
fun bindItem(item: ProductResultData, context: Context){
Glide.with(context)
.load(R.drawable.img_kitchen)
.placeholder(R.drawable.garden_kit)
.into(binding.ivGardenKit)
binding.tvProductName.text = item.product_name
binding.tvPrice.text = "₹" +" "+item.price.toString() + "/ Kit"
binding.tvProductDetails.text = item.about_product
binding.cvProducts.setOnClickListener {
onItemClick.invoke(item)
}
}
}
}
I've been trying to pass data(the email and phone of a user) from my
adapter to my fragment. From what I've read online I should use a
interface for this but I cant I want to use bundle . Can anyone
explain in steps how I should pass data via bundle and how to recieve
it in fragment. Below is my adapter.
class ProductAdapter(
private val onItemClick: (item: ProductResultData) -> Unit,
private val items: List<ProductResultData>
): RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
lateinit var context: Context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
context = parent.context
val binding: ProductItemDataBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.rv_product _item,
parent,
false)
return ProductViewHolder(binding)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val productItem = items[position]
holder.bindItem(productItem,context)
}
override fun getItemCount(): Int {
return items.size
}
inner class ProductViewHolder(private val binding: ProductItemDataBinding): RecyclerView.ViewHolder(binding.root){
@SuppressLint("SetTextI18n")
fun bindItem(item: ProductResultData, context: Context){
Glide.with(context)
.load(R.drawable.img_kitchen)
.placeholder(R.drawable.garden_kit)
.into(binding.ivGardenKit)
binding.tvProductName.text = item.product_name
binding.tvPrice.text = "₹" +" "+item.price.toString() + "/ Kit"
binding.tvProductDetails.text = item.about_product
binding.cvProducts.setOnClickListener {
onItemClick.invoke(item)
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来,适配器的代码还不错。首先,只要您有lambda参数,就必须将其设置为列表的最后一个。
并确保您的
productresultdata
实现包裹可穿线
接口。Your code for the Adapter is alright, it seems. Firstly, whenever you have a lambda parameter, it must be set to the last of the list.
And make sure your
ProductResultData
implementsParcelable
interface.