简体   繁体   English

从另一个 Activity (Kotlin) 将项目添加到 RecyclerView

[英]Add item to RecyclerView from another Activity (Kotlin)

I have the following:我有以下内容:

  • Main Activity with RecyclerView which is filled from Array List带有 RecyclerView 的主 Activity,它是从 Array List 中填充的
  • Adapter (of course)适配器(当然)
  • Another Activity where a new item should be created, added to RecyclerView on Main Activity and then be closed另一个应创建新项目的 Activity,将其添加到 Main Activity 上的 RecyclerView 然后关闭

The main question is, how can I add an item to RecyclerView from another Activity?主要问题是,如何从另一个 Activity 向 RecyclerView 添加项目?

MainActivity Code主要活动代码

 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)
        }

    }

Adapter Code适配器代码

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)
    }
}

Another Activity from which I want to add item to Recyclerview我想将项目添加到 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()
            }
        }
    }

When adding new item from your NewItem activity, pass a bundle adding that item to your MainActivity .从您的NewItem活动添加新项目时,传递一个捆绑包,将该项目添加到您的MainActivity

Then in your MainActivity , get that bundle and set that new item to your RecyclerView.然后在您的MainActivity中,获取该捆绑包并将该新项目设置为您的 RecyclerView。

Use startActivityForResult when starting your NewItem Intent启动 NewItem Intent 时使用startActivityForResult

button.setOnClickListener {
            val intent = Intent(this, NewItem::class.java)
            startActivityForResult(1, intent)
        }

Then in your NewItem activity, call setResult with appropriate resultCode然后在您的 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()

Now override onActivityResult in the main activity like below现在在主要活动中覆盖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)
    }
}

Then finally in your adapter add this method然后最后在你的适配器中添加这个方法

fun addNewItem(newItem: CustomData){
    items.add(newItem)
    notifyItemInserted(items.size-1)
}

Make sure CustomData class implements Serializable thus class CustomData:Serializable{//your code}确保 CustomData class 实现 Serializable因此class CustomData:Serializable{//your code}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击项目时将数据从 recyclerView 传递到新活动 Kotlin - Pass data from recyclerView to new activity when an item is clicked Kotlin Android Kotlin viewBinder - 从 Activity 更改 RecyclerView 项目的 ItemView - Android Kotlin viewBinder - change ItemView of RecyclerView Item from Activity 从另一个活动 kotlin 将数据添加到 listView - add data to a listView from another activity kotlin 从一个 recyclerview 中删除一个项目并将其添加到另一个 - remove an item from one recyclerview and add it to another 如何从另一个 class 添加项目到 RecyclerView - How to add an item to RecyclerView from another class 如何从片段内的特定选定项目 RecyclerView 导航到 Kotlin 中来自同一 RecyclerView 的 2 个不同的 Activity? - How to navigate from specific selected item RecyclerView Inside the Fragment, to 2 different Activity from same RecyclerView in Kotlin? 在其他活动中将项目添加到RecyclerView - Add item to RecyclerView in other Activity 如何将原始 mp3 从 recyclerview 项目传递到另一个活动 - How to pass raw mp3 from recyclerview item to another activity 如何将对象值从RecyclerView项目发送到另一个活动 - How to send object value from RecyclerView Item to Another Activity 如何将价值从recyclerview项目传递到另一个活动 - How to pass value from recyclerview item to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM