数据绑定返回空值

问题描述 投票:0回答:2

我可能做错了所有,但是我在另一个片段/视图模型中具有完全相同的实现,没有问题。也许因为这是一个对话?每次我记录message或message.messagebody时,它都会返回null。谁能指出原因?当前正在学习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>

viewmodel:

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 }
}

}

任何帮助将不胜感激!

android kotlin mvvm data-binding android-databinding
2个回答
0
投票

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


0
投票

通过更改解决

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

to

android:text="@={vm.message.messageBody}
© www.soinside.com 2019 - 2024. All rights reserved.