1
votes

I have the following:

  • Main Activity with RecyclerView which is filled from Array List
  • Adapter (of course)
  • Another Activity where a new item should be created, added to RecyclerView on Main Activity and then be closed

The main question is, how can I add an item to RecyclerView from another Activity?

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


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

1
Once you change Activity, the 1st Activity no longer exists. You need to save your items to a local db! Once you restart the 1st activity it can look up the latest list of items in the db.charles-allen
You should be storing the data in local storage rather than in a random ArrayList in an adapter, refer to codelabs.developers.google.com/codelabs/… (I wanted to link to a different codelabs but Google seems to have removed that one, which is quite unfortunate. This one is far less focused than that one was.)EpicPandaForce
thanks for your replies. I got it to work, i just needed that for testing and now i remade it to use db(room+livedata)Nick Wilde

1 Answers

0
votes

When adding new item from your NewItem activity, pass a bundle adding that item to your MainActivity.

Then in your MainActivity, get that bundle and set that new item to your RecyclerView.