Hello guy i am new to Kotlin android and working on a demo in this demo i am trying to get an image from the gallery and set it in my recyclerview but i am getting the solution requesting you to please find me a solution Thanks in Advance!!

This is my CustomAdapter.kt:-

```
package com.example.itemgetset

import android.app.Activity
import android.app.AlertDialog
import android.content.ContentValues.TAG
import android.content.Intent
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupMenu
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar

@Suppress("UNREACHABLE_CODE")
class CustomAdapter(
    private var activity: Activity,
    private val userList: ArrayList<ProductInfoGetSet>,
    private var isforlist: Boolean,
) :
    RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemview=
            LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
        return ViewHolder(itemview)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val productInfoGetSet: ProductInfoGetSet = userList[position]

        holder.image = productInfoGetSet.image
        holder.txtId.text = productInfoGetSet.id
        holder.txtName.text = productInfoGetSet.name
        holder.txtQuantity.text = productInfoGetSet.quantity
        holder.txtPrice.text = productInfoGetSet.price

        val id = userList[position].id
        Log.e(TAG, "List item ID: $id")

        holder.buttonViewOption.setOnClickListener {

            val popup = PopupMenu(activity, holder.buttonViewOption)
            popup.inflate(R.menu.pop_menu)
            popup.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.edit -> {
                        val intent = Intent(activity, AddDetails::class.java)
                        intent.putExtra("isFor", "Update")
                        intent.putExtra("id", productInfoGetSet.id)
                        intent.putExtra("image",productInfoGetSet.image)
                        intent.putExtra("name", productInfoGetSet.name)
                        intent.putExtra("quantity", productInfoGetSet.quantity)
                        intent.putExtra("price", productInfoGetSet.price)
                        activity.startActivity(intent)
                    }
                    R.id.delete -> {
                        val builder = AlertDialog.Builder(activity)
                        builder.setTitle("Delete")
                        builder.setMessage("Do you want to delete the item?")

                        builder.setPositiveButton("Yes") { _, _ ->
                            userList.removeAt(position)
                            notifyItemRemoved(position)
                            val snack = Snackbar
                                .make(
                                    holder.linearly,
                                    "Item was removed from the list.",
                                    Snackbar.LENGTH_SHORT
                                )
                            snack.show()
                        }
                        builder.setNegativeButton("No") { _, _ ->
                        }
                        val dialog: AlertDialog = builder.create()
                        dialog.show()
                    }
                }
                false
            }
            popup.show()
        }
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

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

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val txtName = itemView.findViewById(R.id.txt_name) as TextView
        val txtId = itemView.findViewById(R.id.txt_id) as TextView
        var image = itemView.findViewById(R.id.imageView2) as ImageView
        val txtQuantity = itemView.findViewById(R.id.txt_quantity) as TextView
        val txtPrice = itemView.findViewById(R.id.txt_price) as TextView
        val linearly: LinearLayout = itemView.findViewById(R.id.linearlayout)
        val buttonViewOption = itemView.findViewById<View>(R.id.txt_Options) as TextView
    }
}

```

in this adapter i gent and error on holder.image = productInfoGetSet.image this line and not get solved.

This is my AddDeatils.kt:-

```
package com.example.itemgetset

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.os.Message
import android.provider.MediaStore
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.add_item.*
import kotlinx.android.synthetic.main.list_layout.*

class AddDetails : AppCompatActivity() {

    private lateinit var btnSubmit: Button
    private lateinit var edtName: EditText
    private lateinit var edtQuantity: EditText
    private lateinit var edtPrice: EditText
    private lateinit var imageview: ImageView

    companion object {
        private const val IMAGE_PICK_CODE = 1000
        private const val PERMISSION_CODE = 1001
    }

    @SuppressLint("SetTextI18n", "ResourceType")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_item)
        supportActionBar?.hide()
        findViewById()
        onclick()

        if (intent.getStringExtra("isFor").equals("Update")) {
            intent.getStringExtra("image")?.toInt()?.let { imageview.setImageResource(it) }
            edtName.setText(intent.getStringExtra("name"))
            edtQuantity.setText(intent.getStringExtra("quantity"))
            edtPrice.setText(intent.getStringExtra("price"))
        }
    }

    private fun findViewById() {
        btnSubmit = findViewById(R.id.btn_submit)
        edtName = findViewById(R.id.edt_name)
        edtQuantity = findViewById(R.id.edt_quantity)
        edtPrice = findViewById(R.id.edt_price)
        imageview = findViewById(R.id.image_view)
    }

    @SuppressLint("ResourceType")
    private fun onclick() {

        btnSubmit.setOnClickListener {
            when {
                edtName.text.trim().isEmpty() -> {
                    edtName.error = "Please Enter Product Name"
                    Toast.makeText(
                        applicationContext,
                        "Please Enter Product Name",
                        Toast.LENGTH_SHORT
                    )
                        .show()
                }
                edtQuantity.text.trim().isEmpty() -> {
                    edtQuantity.error = "Please Enter Product Quantity"
                    Toast.makeText(
                        applicationContext,
                        "Please Enter Product Quantity",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                edtPrice.text.trim().isEmpty() -> {
                    edtPrice.error = "Please Enter Product Price"
                    Toast.makeText(
                        applicationContext,
                        "Please Enter Product Price",
                        Toast.LENGTH_SHORT
                    )
                        .show()
                }
                else -> {
                    Toast.makeText(
                        applicationContext,
                        "Product Added Successfully ",
                        Toast.LENGTH_SHORT
                    ).show()

                    val temp = Temp()
                    temp.image = imageview.toString()
                    temp.name = edtName.text.toString()
                    temp.quantity = edtQuantity.text.toString()
                    temp.price = edtPrice.text.toString()

                    if (intent.getStringExtra("isFor").equals("Update")) {
                        temp.id = intent.getStringExtra("id").toString()
                    }

                    val message: Message = Message.obtain()
                    message.what = 111
                    message.obj = temp
                    MainActivity.handler.sendMessage(message)
                    finish()
                }
            }
        }
        imageview.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) ==
                    PackageManager.PERMISSION_DENIED
                ) {
                    val permissions = arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                    requestPermissions(permissions, PERMISSION_CODE)
                } else {
                    pickImageFromGallery()
                }
            } else {
                pickImageFromGallery()
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        when (requestCode) {
            PERMISSION_CODE -> {
                if (grantResults.isNotEmpty() && grantResults[0] ==
                    PackageManager.PERMISSION_GRANTED
                ) {
                    pickImageFromGallery()
                } else {
                    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    private fun pickImageFromGallery() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        startActivityForResult(intent, IMAGE_PICK_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE) {
            imageview.setImageURI(data?.data)

            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}

```

And this is my MainActivity.kt:-

```
package com.example.itemgetset

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.GridView
import android.widget.LinearLayout
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {

    lateinit var activity: Activity
    val userList = ArrayList<ProductInfoGetSet>()
    private lateinit var btnProductAdd: Button
    lateinit var llEmptyView: LinearLayout
    lateinit var llMain: LinearLayout
    private var listView: ListView? = null
    private var gridView: GridView? = null
    lateinit var recyclerView: RecyclerView
    private lateinit var llFab: LinearLayout
    private lateinit var linearLayoutManager: LinearLayoutManager
    private lateinit var gridLayoutManager: GridLayoutManager
    private lateinit var adapter: CustomAdapter
    private var isforlist = true

    companion object {
        var handler: Handler = Handler()
    }

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

        activity = this
        initView()
        onClicks()
        setUpData()

        handler = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message) {
                if (msg.what == 111) {
                    val temp: Temp = msg.obj as Temp

                    if (temp.id == "") {
                        userList.add(
                            ProductInfoGetSet(
                                (userList.size + 1).toString(),
                                temp.image,
                                temp.name,
                                temp.quantity,
                                temp.price,
                            )
                        )
                        adapter = CustomAdapter(activity, userList, isforlist)
                        recyclerView.adapter = adapter
                    } else {
                        for (i in userList.indices) {
                            if (userList[i].id == temp.id) {
                                userList[i].id = temp.id
                                userList[i].image = temp.image
                                userList[i].name = temp.name
                                userList[i].quantity = temp.quantity
                                userList[i].price = temp.price
                            }
                        }
                        adapter.notifyDataSetChanged()
                    }
                }
                if (userList.size > 0) {
                    llEmptyView.visibility = View.GONE
                    llMain.visibility = View.VISIBLE
                } else {
                    llEmptyView.visibility = View.VISIBLE
                    llMain.visibility = View.GONE
                }
            }
        }
    }

    private fun changeLayoutManager() {
        if (recyclerView.layoutManager == linearLayoutManager) {
            recyclerView.layoutManager = gridLayoutManager
        } else {
            recyclerView.layoutManager = linearLayoutManager
        }
    }

    private fun initView() {
        btnProductAdd = findViewById(R.id.btn_product_add)
        llFab = findViewById(R.id.ll_fab)
        llEmptyView = findViewById(R.id.llEmptyView)
        listView = findViewById(R.id.list_product)
        gridView = findViewById(R.id.list_productGV)
        llMain = findViewById(R.id.llMain)
        recyclerView = findViewById(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this)
        linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        gridLayoutManager = GridLayoutManager(this, 2)
    }

    private fun onClicks() {
        btnProductAdd.setOnClickListener {
            val intent = Intent(this@MainActivity, AddDetails::class.java)
            intent.putExtra("isFor", "Add")
            startActivity(intent)
        }
        llFab.setOnClickListener {
            val intent = Intent(this@MainActivity, AddDetails::class.java)
            intent.putExtra("isFor", "Add")
            startActivity(intent)
        }
    }

    private fun setUpData() {
        if (userList.size > 0) {
            llEmptyView.visibility = View.GONE
            llMain.visibility = View.VISIBLE
        } else {
            llEmptyView.visibility = View.VISIBLE
            llMain.visibility = View.GONE
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.menu_view) {
            if (userList.size > 0) {
                changeLayoutManager()
            }
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}

```

My ProductInfoGetSet.kt:-

```
package com.example.itemgetset

import android.widget.ImageView

class ProductInfoGetSet(
    var id: String,
    var image: ImageView,
    var name: String,
    var quantity: String,
    var price: String
)

```

Temp.kt:-

```
package com.example.itemgetset

import android.widget.ImageView

class Temp {
    var id: String = ""
    var image: ImageView =
    var name: String = ""
    var quantity: String = ""
    var price: String = ""
}

```

Thanks in advance!!