[英]Pass data from recyclerView to new activity when an item is clicked Kotlin
[英]Add item to RecyclerView from another Activity (Kotlin)
我有以下内容:
主要问题是,如何从另一个 Activity 向 RecyclerView 添加项目?
主要活动代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val items = arrayListOf<CustomData>()
items.add(CustomData("Name", R.drawable.icon, 50, "Weekly", "Monday", 1, 50))
button.setOnClickListener {
val intent = Intent(this, NewItem::class.java)
startActivity(intent)
}
itemsListView.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = ItemsAdapter(items)
}
}
适配器代码
class ItemsAdapter(private val items: ArrayList<CustomData>) :
RecyclerView.Adapter<ItemsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.wish_row, parent, false)
val holder = ViewHolder(view)
view.setOnClickListener {
val intent = Intent(parent.context, ItemView::class.java)
intent.putExtra("Name", items[holder.adapterPosition].name)
intent.putExtra("Price", items[holder.adapterPosition].price)
intent.putExtra("Icon", items[holder.adapterPosition].image)
parent.context.startActivity(intent)
}
return holder
}
override fun getItemCount() = items.size
override fun onBindViewHolder(holder: ItemsAdapter.ViewHolder, position: Int) {
holder.name.text = items[position].name
holder.price.text = items[position].price.toString()
holder.image.setImageDrawable(holder.image.context.getDrawable(items[position].image))
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image: ImageView = itemView.findViewById(R.id.imageView)
val name: TextView = itemView.findViewById(R.id.textView)
val price: TextView = itemView.findViewById(R.id.textView2)
}
}
我想将项目添加到 Recyclerview 的另一个 Activity
class NewItem : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.new_item_activity)
val items = arrayListOf<CustomData>()
items.add(CustomData("Name2", R.drawable.icon, 50, "Weekly", "Monday", 2, 50))
newItemButton.setOnClickListener {
itemsListView.layoutManager = LinearLayoutManager(this)
itemsListView.adapter = itemsAdapter(items)
finish()
}
}
}
从您的NewItem
活动添加新项目时,传递一个捆绑包,将该项目添加到您的MainActivity
。
然后在您的MainActivity
中,获取该捆绑包并将该新项目设置为您的 RecyclerView。
启动 NewItem Intent 时使用startActivityForResult
button.setOnClickListener {
val intent = Intent(this, NewItem::class.java)
startActivityForResult(1, intent)
}
然后在您的 NewItem 活动中,使用适当的 resultCode 调用 setResult
// on success
val returnIntent = Intent()
returnIntent.putExtra("result",result) //result is data you want to add to recycler
setResult(Activity.RESULT_OK,returnIntent)
finish()
现在在主要活动中覆盖onActivityResult
,如下所示
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val newItem = data.getSerializableExtra("result") as CustomData
adapter.addNewItem(newItem)
}
}
然后最后在你的适配器中添加这个方法
fun addNewItem(newItem: CustomData){
items.add(newItem)
notifyItemInserted(items.size-1)
}
确保 CustomData class 实现 Serializable因此class CustomData:Serializable{//your code}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.