title: Android: add a new item to a list and display it via RecyclerView adapter using Kotlin
body: <p>I have a fragment that has a RecyclerView. When the user clicks a button (ADD NEW ITEM) in the fragment, the app takes the user (via intent) to a form to fill-in some data.</p>
  
  <p>When I click on (ADD ITEM), it should add the data to the array list and finish that screen. After going back to the fragment, this data should be added to the RecyclerView using the adapter.</p>
  
  <p>This is my code:</p>
  
  <p>Here is the fragment:</p>
  
  <pre><code>private val REQUEST_CODE = 2
  
  internal lateinit var adapter: ShipmentItemAdapter
  
  override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
      return inflater!!.inflate(R.layout.fragment_step_three_shipment, container, false)
  }
  
  override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
  
      btnAddNewShipmentStep3.setOnClickListener {
          val intent = Intent(context, AddItemActivity::class.java)
          startActivityForResult(intent, REQUEST_CODE)
      }
  }
  
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data);
  
      val items = ArrayList&lt;ItemsItem&gt;()
  
      if (requestCode == REQUEST_CODE) {
  
          adapter = ShipmentItemAdapter(context, items)
  
          if (resultCode == Activity.RESULT_OK) {
              itemsShipments.visibility = View.VISIBLE
              itemsShipments.layoutManager = LinearLayoutManager(context)
              itemsShipments.setHasFixedSize(true)
              itemsShipments.adapter = adapter
  
          } else if (resultCode == Activity.RESULT_CANCELED) {
              itemsShipments.visibility = View.GONE
          }
      }
  }
  </code></pre>
  
  <p>And here is the activity:</p>
  
  <pre><code>class AddItemActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_add_item_form)
  
      linPackageAddShipment.setOnClickListener {
          edtPackageAddShipment.requestFocus()
      }
  
      linPiecesAddShipment.setOnClickListener {
          edtPiecesAddShipment.requestFocus()
      }
  
      linWeightAddShipment.setOnClickListener {
          edtWeightAddShipment.requestFocus()
      }
  
      linDescriptionAddShipment.setOnClickListener {
          edtDescriptionAddShipment.requestFocus()
      }
  
  
      btnAddItemAddShipment.setOnClickListener {
          val errors = collectData()
  
          collectData()
  
          if (errors.isEmpty()) {
  
              val intent = Intent()
              setResult(Activity.RESULT_OK, intent)
              finish()
  
          } else {
              val errorBody = errors.map { getString(it) }.joinToString("\n") { it }
              MaterialDialog.Builder(this)
                      .title("Oops")
                      .content(errorBody)
                      .positiveText(R.string.ok)
                      .positiveColor(ContextCompat.getColor(this, R.color.colorPrimary))
                      .onAny({ dialog, _ -&gt; dialog.dismiss() })
                      .show()
          }
      }
    }
  
    private fun collectData(): ArrayList&lt;Int&gt; {
      val errors = ArrayList&lt;Int&gt;()
      val item = ItemsItem()
  
      if (edtPackageAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.packages_must_not_be_empty)
  
      } else {
          item.numberOfPackages = edtPackageAddShipment.text.toString().toInt()
      }
  
      if (edtPiecesAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.pieces_must_not_be_empty)
  
      } else {
          item.numberOfPieces = edtPiecesAddShipment.text.toString()
      }
  
      if (edtWeightAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.weight_must_not_be_empty)
  
      } else {
          item.weightUnit = edtWeightAddShipment.text.toString().toInt()
      }
  
      if (edtHeightAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.height_must_not_be_empty)
  
      } else {
          item.height = edtHeightAddShipment.text.toString()
      }
  
      if (edtWidthAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.width_must_not_be_empty)
  
      } else {
          item.width = edtWidthAddShipment.text.toString()
      }
      if (edtLengthAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.length_must_not_be_empty)
  
      } else {
          item.length = edtLengthAddShipment.text.toString()
      }
  
      if (edtDescriptionAddShipment.text.toString().isEmpty()) {
          errors.add(R.string.description_must_not_be_empty)
  
      } else {
          item.description = edtDescriptionAddShipment.text.toString()
      }
  
      CreateNewShipmentActivity.mainShipment.shipmentObj.items?.add(item)
  
      return errors
    }
  
    override fun onBackPressed() {
      setResult(Activity.RESULT_CANCELED)
      super.onBackPressed()
    }
  }
  </code></pre>
  
  <p>Here is the adapter:</p>
  
  <pre><code>class ShipmentItemAdapter internal constructor(private val context: Context, private val items: List&lt;ItemsItem&gt;) : RecyclerView.Adapter&lt;ShipmentItemAdapter.MyViewHolder&gt;() {
  
    private val inflater: LayoutInflater = LayoutInflater.from(context)
  
  
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
      val view = inflater.inflate(R.layout.item_shipment_package, parent, false)
  
      return MyViewHolder(view).listen { pos, type -&gt;
          val item = items[pos]
          val intent = Intent(context, AddItemActivity::class.java)
          startActivity(context, intent, null)
      }
    }
  
  
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
      holder.namePackageShipmentPackage.text = items[position].description
      holder.quantityPackageShipmentPackage.text = items[position].description
      holder.dimensionPackageHeightShipmentPackage.text = items[position].description
      holder.dimensionPackageWidthShipmentPackage.text = items[position].description
      holder.dimensionPackageLengthShipmentPackage.text = items[position].description
  
    }
  
    override fun getItemCount(): Int {
      return items.size
    }
  
    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  
      var namePackageShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.namePackageShipmentPackage)
      var quantityPackageShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.quantityPackageShipmentPackage)
      var dimensionPackageHeightShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.dimensionPackageHeightShipmentPackage)
      var dimensionPackageWidthShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.dimensionPackageWidthShipmentPackage)
      var dimensionPackageLengthShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.dimensionPackageLengthShipmentPackage)
  
  
    }
  
    private fun &lt;T : RecyclerView.ViewHolder&gt; T.listen(event: (position: Int, type: Int) -&gt; Unit): T {
      itemView.setOnClickListener {
          event.invoke(adapterPosition, itemViewType)
      }
      return this
    }
  </code></pre>
  
  <p>These are the object model:</p>
  
  <pre><code>class ShipmentObj {
      var items: ArrayList&lt;ItemsItem?&gt;? = null
  }
  
  
  data class ItemsItem(
      var numberOfPieces: String? = null,
      var length: String? = null,
      var description: String? = null,
      var numberOfPackages: Int? = null,
      var number: Int? = null,
      var width: String? = null,
      var height: String? = null,
      var weightUnit: Int? = null
  )
  </code></pre>
  
  <p>When I try to add new item nothing is showing in the fragment!
  I don't now where is the mistake?</p>
  
  <p>Thanks.</p>