你好。。。我坚持将数据从适配器回收器视图传递到片段



这是我的RecyclerView代码:


class RecyclerAdapterMain(val product: ArrayList<ModelProductMain>) :
RecyclerView.Adapter<RecyclerAdapterMain.ViewHolder>() {

class ViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview) {

val title: TextView = itemview.product_txt
val price: TextView = itemview.price_product
val imageproduct: ImageView = itemview.product_image
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutview =
LayoutInflater.from(parent.context).inflate(R.layout.product_items, parent, false)
return ViewHolder(layoutview)

}
override fun getItemCount() = product.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val products = product[position]
holder.title.text = products.title
holder.price.text = products.price.toString()

holder.itemView.setOnClickListener {

val activity = it.context as AppCompatActivity
activity.supportFragmentManager.beginTransaction()
.replace(R.id.homepage, ItemDetailsfragment())
.addToBackStack(null)
.commit()
val bunde = Bundle()
bunde.putString("title", products.title)

}
}
}

这是我想要传递数据的片段:

class ItemDetailsfragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.details_items_page, container, false)
val title = Bundle()
val get = title.getString("title")
d("main", "$get")

return view
}
}

当我试图从Recyclerview适配器获取数据时,我只得到了null参数。我尝试了这样的事情,同时调试了我的程序,它说来的变量是空的。我被这个卡住了,我不知道这个怎么了。。。有人能处理这个吗。。。?

将值传递给片段时:

val bundle = Bundle()
bundle.putString("title", products.title)
val myFragment = ItemDetailsfragment()
myFragment.arguments = bundle

activity.supportFragmentManager.beginTransaction()
.replace(R.id.homepage, myFragment)
.addToBackStack(null)
.commit()

检索值时:

val temp = arguments?.getString("title")

相关内容

最新更新