0

I have a DialogFragment that I want to show when clicking on an item inside a Recycler View. Here are my files:

DialogFragment.kt:

class TaskDialogFragment:DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var rootView: View = inflater.inflate(R.layout.task_dialog, container, false)
        return  rootView

    }
}

RecyclerViewAdapter.kt:

class RecycleViewAdapter(): RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>() {

    private lateinit var dao: TaskDao
    private lateinit var dialogFragment: TaskDialogFragment

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        init {
            itemView.setOnClickListener { v: View ->
                // this is where I want my dialogFragment to be opened from
                dialogFragment = TaskDialogFragment()
                dialogFragment.show(supportFragmentManager, "taskDialog")
            }
        }
    }

I have tried following the steps shown here https://developer.android.com/guide/fragments/dialogs but I get an error saying Unresolved Reference: supportFragmentManager.

How would I open this dialog from the recycler view? Any tips would be appreciated.

Roh
  • 311
  • 1
  • 10
  • You can use callback for it or pas activity refrence to adapter then dialogFragment.show(mActivity.supportFragmentManager,"taskDailog") – Ammar Abdullah Jul 20 '22 at 04:28
  • No I still get the same error – Roh Jul 20 '22 at 04:30
  • can you post code how are you doing? – Ammar Abdullah Jul 20 '22 at 04:32
  • Sorry I misread your comment. How would I pass activity reference to adapter – Roh Jul 20 '22 at 04:33
  • From activity val adapter=RecycleViewAdapter(this) From Fragment val adapter=RecycleViewAdapter(requireActivity()) – Ammar Abdullah Jul 20 '22 at 04:34
  • The better approach for this issue is to create a callback (i.e interface) for your item click, pass that callback in the constructor of your adapter, implement that interface in your activity/fragment and then invoke that callback in a click of your view in the adapter. Find it on google, you will find many examples for that. – Bhavnik Jul 20 '22 at 05:26

0 Answers0