对不起,我不能直接将图片发布到帖子中,因为它说我必须至少有10个声誉才能发布它。
我在android studio中创建了一个xml,就像这样的Xml。
并在视图模型中创建了2个变量,如下所示
private val _loadingText = MutableLiveData<String>()
val loadingText: LiveData<String> = _loadingText然后在我的对话视图中实现数据绑定,如下所示
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)但是当我运行代码时,它显示了如下错误
我不知道该怎么解决这个问题..请帮帮我。
更新:当我运行--stacktrace时,我仍然不知道这是什么错误。
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
发布于 2020-04-25 20:19:10
您正在使用双向数据绑定,这在这里是不正确的。
将android:text="@={viewModel.loadingText}"更改为android:text="@{viewModel.loadingText}"
有关该问题的更多信息:当您还希望从UI更新数据时,将使用双向数据绑定。案例可以是将其文本发布到MutableLiveData并从中设置其文本的EditText。有关更多详细信息,请查看official documentation。
发布于 2020-04-25 20:49:23
请尝试此代码以使用自定义对话框实现数据绑定。它对我来说运行得很好
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()
}https://stackoverflow.com/questions/61424074
复制相似问题