数据绑定返回 null

Data binding returning null

我可能做错了,但我在另一个 fragment/viewmodel 中有完全相同的实现,没有问题。也许是因为它是一个对话?每次我记录消息或 message.messagebody 它 returns 空。谁能指出为什么?目前正在学习mvvm。

xml:(很重要,因为它很长)

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="user"
            type="com.catbellystudio.knodee.models.Users" />

        <variable
            name="vm"
            type="com.catbellystudio.knodee.ui.profile.ProfileViewModel" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_margin="10dp"
        android:background="@drawable/custom_background_popup"
        android:elevation="10dp"
        android:orientation="vertical">

                <ScrollView
                    android:id="@+id/popupTextLayout"
                    android:layout_width="match_parent"
                    android:layout_height="277dp"
                    android:layout_marginTop="8dp">

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorPrimary"
                        android:hint="@string/your_message"
                        android:inputType="textMultiLine"
                        android:padding="10dp"
                        android:text="@{vm.message.messageBody}" />
                </ScrollView>
    </LinearLayout>
</layout>

视图模型:

class ProfileViewModel(
private val userRepository: UserRepository,
private val messageRepository: MessageRepository
) : ViewModel() {

var message: Message = Message()
var sender: Users? = null
var receiver: Users? = null
var string:String?=null

fun getLoggedInUser() = runBlocking { userRepository.getUser() }

fun onBackPressed(view: View) {
    Navigation.findNavController(view).navigateUp()
}

fun postMessage(view:View) {
    Coroutines.main {
        Log.e("messagevm", message.toString())
    }
}
}

片段:

class MessageFragment : Fragment(), KodeinAware {

private lateinit var viewModel: ProfileViewModel
private lateinit var profileBinding: FragmentProfileBinding
private lateinit var popupBinding: FragmentPopupBinding
override val kodein by kodein()
private val factory: ProfileViewModelFactory by instance()

private lateinit var dialog: Dialog

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    viewModel = ViewModelProviders.of(this, factory).get(ProfileViewModel::class.java)

profileBinding = DataBindingUtil.inflate(
    inflater,
    R.layout.fragment_profile,
    container,
    false
)

popupBinding = FragmentPopupBinding.inflate(LayoutInflater.from(context))
dialog = context?.let { Dialog(it) }!!
dialog.setContentView(popupBinding.root)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

profileBinding.viewModel = viewModel
popupBinding.vm = viewModel

getSender()

return profileBinding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

settingsButtonProfile.visibility = View.GONE

messageButtonProfile.setOnClickListener {
    showPopUp()
}

val receiver by lazy {
    arguments?.let { fromBundle(it).user }
}

viewModel.receiver = receiver

}

private fun showPopUp() {
dialog.show()

val switch = dialog.visibilitySwitchPopup
val visibilityTextView = dialog.visibilityTextViewPopup

dialog.closeButtonPopUp?.setOnClickListener {
    dialog.dismiss()
}

switch?.setOnClickListener {
    val isIconEnabled = switch.isIconEnabled
    if (isIconEnabled) {
        visibilityTextView?.text = getString(R.string.anonymous_prompt)
    } else {
        visibilityTextView?.text = getString(R.string.anonymous_warning)
    }
    switch.switchState()
}

}

private fun getSender() {
viewModel.getLoggedInUser()?.let { viewModel.sender = it }
}

}

如有任何帮助,我们将不胜感激!

将此 "popupBinding.vm = viewModel" 行移至 onViewCreated() 方法并在同一方法中包含此行 "popupBinding.lifeCycleOwner=this"

通过更改

解决
android:text="@{vm.message.messageBody}

android:text="@={vm.message.messageBody}