数据绑定返回空值

时间:2019-11-25 14:42:53

标签: android kotlin mvvm data-binding android-databinding

我可能做错了所有,但是我在另一个片段/视图模型中具有相同的确切实现,没有问题。也许因为这是一个对话?每次我记录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 }
}

}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

通过更改解决

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

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