1

Sorry I cannot post image directly to the post, because it said that I must have at least 10 reputation to post it.

I created an xml in android studio like this Xml

and created 2 variable inside view model like this

private val _loadingText = MutableLiveData<String>()
val loadingText: LiveData<String> = _loadingText

then implement the data binding like this into my dialog view

val dialogView = layoutInflater.inflate(R.layout.dialog_custom_loading, dialog_root)
    val binding = DialogCustomLoadingBinding.inflate(layoutInflater, dialogView as ViewGroup, false)
    binding.viewModel = viewModel
    loading = Dialog(this)
    loading.setContentView(binding.root)

but when i run the code, it shown an error like this

Error

and i dont know, how to solve it.. help me please..

UPDATE : when i run with --stacktrace i still dont know what error is this..

The expression 'viewModelLoadingText.getValue()' cannot be inverted, so it cannot be used in a two-way binding

Details: There is no inverse for method getValue, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions

Garalax
  • 13
  • 1
  • 5

2 Answers2

4

You are using two-way databinding which is not correct in this place.

Change android:text="@={viewModel.loadingText}" to android:text="@{viewModel.loadingText}"

More information about the issue: Two-way databinding is used when you also want your data to be updated from UI. A case may be an EditText which posts its text to a MutableLiveData and sets its text from it. Check out the official documentation for more details.

momvart
  • 1,737
  • 1
  • 20
  • 32
  • Wogh.. idk why, previously I used LiveData and then use this at my xml `android:text="@={viewModel.loadingText}"` But then i change the loading text data type to Mutable Live data, but stil use the "@=". And it works.. but why? – Garalax Apr 26 '20 at 06:13
  • Because when you use two-way data binding the UI must be able to mutate the data which is not possible with `LiveData`. So if you use `MutableLiveData` instead you give access to outerworld (UI) to change your data and errors disappear. @Garalax – momvart Apr 26 '20 at 07:08
  • 1
    so when i want to use `LiveData` it must be a one way data binding? so i must use `"@{}"`?. Or, when i want to bind my data to view, it must be a two way data binding? – Garalax Apr 26 '20 at 07:13
  • 1
    yeah, it must be a one way data binding. as I mentioned, you use a two-way data binding when you want your data to be updated by user. for example, we can use a two-way data binding for an `EditText` text attribute, but when it is a `TextView` and we want to only show data one-way data binding is used. @Garalax – momvart Apr 26 '20 at 07:20
0

Please try this code to implement databinding with custom dialog. It is working fine for me

    private fun showAddContactDialog() {
    dialog= Dialog(this)

    dialog.setCancelable(true)
    dialog.setCanceledOnTouchOutside(true)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    Objects.requireNonNull(dialog.window)!!.setBackgroundDrawable(
        ColorDrawable(Color.TRANSPARENT)
    )
    dialog.window!!.setGravity(Gravity.CENTER)

    val dialogAddNotesBinding:DialogAddNotesBinding =DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.dialog_add_notes,null,false)
    homeViewModel= ViewModelProviders.of(this).get(HomeViewModel::class.java)
    dialogAddNotesBinding.homeModel=homeViewModel
    homeViewModel.addNewContactListener=this
    dialog.setContentView(dialogAddNotesBinding.root)
    val window = dialog.window
    window!!.setLayout(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
    )
    dialog.show()
}
white hills
  • 237
  • 1
  • 8