'How to add items to RecyclerView from EditText, ImageView with onClickListener?

so I want to make an app, where you can add data such as Image, Video, description and Title from PopupWindow, then by clicking AddButton it will add provided data to recyclerView. I know I need to use notifyItemInserted but i don't know how. The Image will be a thumbnail, Video will be not visible, it will work only on the next fragment with player (i will do it later).

here is a preview of popupWindow and recyclerView behind it.

RecyclerAdapter.kt

class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {

private var dataList = emptyList<ListData>()

class MyViewHolder(val binding: CardLayoutBinding): RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    return MyViewHolder(CardLayoutBinding.inflate(LayoutInflater.from(parent.context), parent,false))
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.binding.tvTitle.text = dataList[position].title
    holder.binding.tvDescription.text = dataList[position].description
    holder.binding.tvThumbnail // what to do next?
}

override fun getItemCount(): Int {
    return dataList.size
     }
  }

ListData.kt

data class ListData(
val id: Int,
val title: String,
val description: String,
val image: ImageView,
val Video: VideoView
)

MainActivity.kt

class MainActivity : AppCompatActivity() {
private var dialogView: View? = null

private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
    it?.let { uri ->
        dialogView?.findViewById<ImageView>(R.id.imageChange)?.setImageURI(it)
    }?:run {
        Log.e("MainActivity", "URI not present")
    }
})

private val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
it?.let { uri ->
    dialogView?.findViewById<VideoView>(R.id.videoChange)?.setVideoURI(it)
}?: run{
    Log.e("MainActivity", "URI not present")
    }
})

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    supportActionBar?.hide()

    bottomNavigationView.background = null
    bottomNavigationView.menu.findItem(R.id.placeholder).isEnabled = false
    replaceFragment(HomeFragment())

    bottomNavigationView.setOnItemSelectedListener {
        when (it.itemId) {
            R.id.home -> replaceFragment(HomeFragment())
            R.id.player -> replaceFragment(PlayerFragment())
            R.id.profile -> replaceFragment(ProfileFragment())
            R.id.settings -> replaceFragment(SettingsFragment())
        }
        true
    }

    popupAddButton.setOnClickListener {
        showDialog()
    }
}


private fun replaceFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.fragment_container, fragment)
    transaction.commit()
}
private fun showDialog() { //this is for popupWindow
    dialogView = layoutInflater.inflate(R.layout.popup, null)
    val dialog = Dialog(this)
    val titleEditText = dialogView?.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
    val descEditText = dialogView?.findViewById<EditText>(R.id.description) //popUp edit field description

    dialogView?.addImage?.setOnClickListener {
        getPreviewImage.launch("image/*")

    }
    dialogView?.addVideo?.setOnClickListener {
        getPreviewVideo.launch("video/*")
    }

    dialogView?.addButton?.setOnClickListener {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
        if (titleEditText?.text?.isEmpty() == true || descEditText?.text?.isEmpty() == true){
            Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
        }
    }
    dialog.setContentView(dialogView!!)
    dialog.show()
   }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source