title: How to retrive data from DialogFragment with ViewModel and Room
body: <p>I'm building an Android app that has different pages that mainly have some EditText. My goal is to handle the click on the EditText and shows a DialogAlert with an EditText, then the user can put the text, click &quot;save&quot; and the related field in the database (I'm using Room and I've tested the queries and everything works) will be updated. Now I was able to handle the text from the DialogFragment using interface but I don't know how to say that the text retrieved is related to the EditText that I've clicked. What is the best approach to do this?
  Thanks in advance for your help.</p>
  <p>Let's take this fragment as example:</p>
  <pre><code>class StaticInfoResumeFragment : Fragment(), EditNameDialogFragment.OnClickCallback {
  
  private val wordViewModel: ResumeStaticInfoViewModel by viewModels {
      WordViewModelFactory((requireActivity().application as ManagementCinemaApplication).resumeStaticInfoRepo)
  }
  
  override fun onCreateView(
      inflater: LayoutInflater,
      container: ViewGroup?,
      savedInstanceState: Bundle?,
  ): View? {
      val root = inflater.inflate(R.layout.fragment_static_info_resume, container, false)
  
      wordViewModel.resumeStaticInfo.observe(viewLifecycleOwner) { words -&gt;
          println(&quot;test words: $words&quot;)
      }
  
      val testView = root.findViewById&lt;TextInputEditText&gt;(R.id.textInputEditText800)
  
  
      testView.setOnClickListener{
          val fm: FragmentManager = childFragmentManager
          val editNameDialogFragment = EditNameDialogFragment.newInstance(&quot;Some Title&quot;)
          editNameDialogFragment.show(fm, &quot;fragment_edit_name&quot;)
      }
  
      resumeStaticInfoViewModel.firstName.observe(viewLifecycleOwner, Observer {
          testView.setText(it)
      })
  
      return root
  }
  
  override fun onClick(test: String) {
      println(&quot;ciao test: $test&quot;)
      wordViewModel.updateFirstName(testa)
  }}
  </code></pre>
  <p>Then I've the ViewModel:</p>
  <pre><code>class ResumeStaticInfoViewModel(private val resumeStaticInfoRepo: ResumeStaticInfoRepo): ViewModel() {
  
  val resumeStaticInfo: LiveData&lt;ResumeStaticInfo&gt; = resumeStaticInfoRepo.resumeStaticInfo.asLiveData()
  
  fun updateFirstName(resumeStaticInfoFirstName: String) = viewModelScope.launch {
      resumeStaticInfoRepo.updateFirstName(resumeStaticInfoFirstName)
  }
  ....
  </code></pre>
  <p>And the DialogFragment:</p>
  <pre><code>class EditNameDialogFragment : DialogFragment() {
  
  private lateinit var callback: OnClickCallback
  
  interface OnClickCallback {
      fun onClick(test: String)
  }
  
  override fun onAttach(context: Context) {
      super.onAttach(context)
      try {
          callback = parentFragment as OnClickCallback
      } catch (e: ClassCastException) {
          throw ClassCastException(&quot;$context must implement UpdateNameListener&quot;)
      }
  }
  
  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
      val title = requireArguments().getString(&quot;title&quot;)
      val alertDialogBuilder: AlertDialog.Builder = AlertDialog.Builder(requireContext())
      alertDialogBuilder.setTitle(title)
      val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
      val alertCustomView = layoutInflater.inflate(R.layout.alert_dialog_edit_item, null)
      val editText = alertCustomView.findViewById&lt;EditText&gt;(R.id.alert_edit)
      alertDialogBuilder.setView(alertCustomView)
  
      alertDialogBuilder.setPositiveButton(
          &quot;Save&quot;,
          DialogInterface.OnClickListener { dialog, which -&gt;
              callback.onClick(editText.text.toString())
          })
      alertDialogBuilder.setNegativeButton(&quot;No&quot;) { _: DialogInterface, _: Int -&gt; }
      return alertDialogBuilder.create()
  }
  
  companion object {
      fun newInstance(title: String?): EditNameDialogFragment {
          val frag = EditNameDialogFragment()
          val args = Bundle()
          args.putString(&quot;title&quot;, title)
          frag.arguments = args
          return frag
      }
  }
  </code></pre>
  <p>}</p>