0

I have an image button in my recylerview and when users click it, I want a dialog box to pop up and allow the user to edit the data in the reyclerview and save the changes.

enter image description here

My Adapter code

class Adapter(private var records: ArrayList<AudioRecord>, var listener: OnItemClickListener) : RecyclerView.Adapter<Adapter.ViewHolder>() {

private var editMode = false

fun isEditMode() :Boolean{return editMode}
@SuppressLint("NotifyDataSetChanged")
fun setEditMode(mode: Boolean){
    if(editMode != mode){
        editMode = mode
        notifyDataSetChanged()
    }
}

inner class ViewHolder(val binding: ItemviewLayoutBinding): RecyclerView.ViewHolder(binding.root ), View.OnClickListener, View.OnLongClickListener{
    private var tvFileName : TextView = itemView.findViewById(R.id.tvFilename)
    private var tvMeta : TextView = itemView.findViewById(R.id.tvMeta)
    var checkbox : CheckBox = itemView.findViewById(R.id.checkBox)
    val editBtn: ImageButton = itemView.findViewById(R.id.btnEdit)



    init {

        itemView.setOnClickListener(this)
        itemView.setOnLongClickListener(this)
    }

    fun binding (audioRecord: AudioRecord) {

        tvFileName.text = audioRecord.filename
        tvMeta.text = audioRecord.duration

        // checkbox.text = audioRecord.

    }

    override fun onClick(p0: View?) {
        val position = adapterPosition
        if(position != RecyclerView.NO_POSITION)
            listener.onItemClickListener(position)
    }

    override fun onLongClick(p0: View?): Boolean {
        val position = adapterPosition
        if(position != RecyclerView.NO_POSITION)
            listener.onItemLongClickListener(position)

        return true
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(ItemviewLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))

}

@SuppressLint("SetTextI18n", "SimpleDateFormat")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    if (position != RecyclerView.NO_POSITION){

        val record: AudioRecord = records[position]
        val sdf = SimpleDateFormat("dd/MM/yyyy")
        val date = Date(record.timestamp)
        val strDate = sdf.format(date)

        holder.binding.tvFilename.text = record.filename
        holder.binding.tvMeta.text = "${record.duration} $strDate"

        if(editMode){
            holder.checkbox.visibility = View.VISIBLE
            holder.checkbox.isChecked = record.isChecked
            holder.editBtn.visibility = View.GONE
        }else{
            holder.checkbox.visibility = View.GONE
            holder.checkbox.isChecked = false
            holder.editBtn.visibility = View.VISIBLE

        }

        holder.binding.btnEdit.setOnClickListener {
            
        }
    }
}
override fun getItemCount(): Int {
    return records.size
}

}

Summary

  1. When users click image button. Input dialog pops up
  2. User must be able to edit data in recyclerview and save it.
Khumo Mashapa
  • 390
  • 1
  • 4
  • 13

1 Answers1

0

change class like this inner class ViewHolder(val binding: ItemviewLayoutBinding), var imageListener:(position:Int)->Unit)

where you call adapter , use imageListener function. Whatever you want to do you can do it inside this function. Then call it in your adapter.

in init

editBtn.setOnClickListener {
  imageListener(adapterPosition)
}
Talha Saglam
  • 17
  • 1
  • 5