[英]Pass data from recyclerView to new activity when an item is clicked Kotlin
我有一个 recyclerView,它使用来自 firebase 数据库的数据在 cardview 中显示项目。 我编写了在单击某个项目时启动新活动的编码。 现在我想将从 recyclerView 中点击的项目的数据传递给新的活动,以便我可以在其中显示数据。 我正在使用 kotlin。我在 yt 上看的视频很少,尝试的方法也很少。 因为我是初学者,所以我需要帮助来解决这个问题。 提前致谢。
我有一个 RecyclerItemClickListener,它在单击项目时启动新活动。
回收站查看 class
class storekotlin : Fragment() {
private var param1: String? = null
private var param2: String? = null
var recyclerView: RecyclerView? = null
var productlist = ArrayList<Product>()
private var database: FirebaseDatabase? = null
private var reference: DatabaseReference? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_storekotlin, container, false)
database = FirebaseDatabase.getInstance()
reference = database?.getReference("Products")
val FirebaseListener = object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
val child = p0.children
child.forEach{
var products = Product(it.child("img").value.toString(),
it.child("name").value.toString(), it.child("price").value.toString())
productlist.add(products)
}
val adapter = ProductAdapter(productlist)
recyclerView?.adapter = adapter
}
override fun onCancelled(p0: DatabaseError) {
}
}
reference?.addValueEventListener(FirebaseListener)
recyclerView = view.findViewById(R.id.recyclerview)
recyclerView?.setHasFixedSize(true)
recyclerView?.layoutManager = GridLayoutManager(activity, 1,
GridLayoutManager.VERTICAL, false)
val defuserimg: ImageView = view.findViewById(R.id.defuserimg)
val defuser: TextView = view.findViewById(R.id.defuser)
val info: ImageView = view.findViewById(R.id.info)
val search: EditText = view.findViewById(R.id.search)
defuserimg.setOnClickListener {
val intent = Intent(activity, signin_page::class.java)
startActivity(intent)
}
defuser.setOnClickListener {
val intent = Intent(activity, signin_page::class.java)
startActivity(intent)
}
info.setOnClickListener {
val intent = Intent(activity, aboutpage::class.java)
startActivity(intent)
}
recyclerView?.addOnItemTouchListener(
RecyclerItemClickListener(
activity, recyclerView!!, object :
RecyclerItemClickListener.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
val intent = Intent(activity, productdetails::class.java)
startActivity(intent)
}
override fun onLongItemClick(view: View, position: Int) {
}
})
)
return view
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
storekotlin().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
回收器适配器 class
class ProductAdapter(private var productlist:MutableList<Product>):
RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ProductViewHolder {
val layoutView:View =
LayoutInflater.from(parent.context).inflate(R.layout.itemcard,parent,false)
return ProductViewHolder(layoutView)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
Picasso.get().load(productlist[position].prdimg).into(holder.prdimg)
holder.prdname.text = productlist[position].prdname
holder.prdprice.text = productlist[position].prdprice
}
override fun getItemCount(): Int {
return productlist.size;
}
inner class ProductViewHolder(view: View): RecyclerView.ViewHolder(view){
var prdimg: ImageView = view.findViewById(R.id.prdimg)
var prdname: TextView = view.findViewById(R.id.prdname)
var prdprice: TextView = view.findViewById(R.id.prdprice)
}
最简单的方法
在适配器 class 中喜欢
class ProductAdapter(val fragment: storekotlin,private var productlist:MutableList<Product>):
RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ProductViewHolder {
val layoutView:View =
LayoutInflater.from(parent.context).inflate(R.layout.itemcard,parent,false)
return ProductViewHolder(layoutView)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
Picasso.get().load(productlist[position].prdimg).into(holder.prdimg)
holder.prdname.text = productlist[position].prdname
holder.prdprice.text = productlist[position].prdprice
holder.itemView.setOnClickListener {
val name = productlist[position].prdname
val price= productlist[position].prdprice
fragment.deatailsList(name , price) // here you can send just the position it will be more efficient
}
}
override fun getItemCount(): Int {
return productlist.size;
}
inner class ProductViewHolder(view: View): RecyclerView.ViewHolder(view){
var prdimg: ImageView = view.findViewById(R.id.prdimg)
var prdname: TextView = view.findViewById(R.id.prdname)
var prdprice: TextView = view.findViewById(R.id.prdprice)
}
在 storekotlin Fragment onCreateView 方法中传递这样的列表和片段
val adapter = ProductAdapter(this@storekotlin,productlist)
recyclerView?.adapter = adapter
在 storekotlin 活动中创建另一种方法
fun deatailsList(name: String, price: String?) {
// Log.d(ContentValues.TAG, "myList:${dataList.size}")
val intent = Intent(requireContext(), DetailsActivity::class.java)
intent.putExtra("nameKey", name)
intent.putExtra("priceKey", price)
startActivity(intent)
}
在详细信息活动的 onCreate 方法中
val intent = intent
val rcvName = intent.getStringExtra("nameKey")
val rcvPrice= intent.getStringExtra("priceKey")
// now you can print here the selected value
textDayIncome_id.text = rcvName
textDayExpense_id.text = rcvPrice
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.