简体   繁体   English

如何更改 kotlin 中的实时数据(字符串)? 您正在使用视图模型

[英]How to change live data(String) in kotlin? You are using a viewmodel

I want to change the live data of the view model and output it to the view.我想将视图 model 和 output 的实时数据更改为视图。 I set the string as the initial value, and after that, I try to change the string by executing a function in the view model, but it does not change.我将字符串设置为初始值,然后尝试通过在视图 model 中执行 function 来更改字符串,但它没有改变。 Is there something wrong??有什么不对??


ViewModel.kt视图模型.kt

class MainViewModel: ViewModel() {
    private var _title_1 = MutableLiveData<String>()

init {
    _title_1.value = "PISO NOBLE"
    }


val title_1: MutableLiveData<String>
    get() = _title_1

fun changeCat () {
    _title_1.value = "PISO NOBRE" }

fragement_page.xml片段页面.xml

<TextView
                    android:id="@+id/pageOneTitle_1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/white"
                    android:textSize="24dp" />


<com.google.android.material.button.MaterialButton
            android:id="@+id/langCat"
            android:layout_width="160dp"
            android:layout_height="match_parent"
            style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
            app:cornerRadius="28dp"
            android:onClick="@{()->mainViewModel.changeCat()}"
            android:text="CAT"
            android:textSize="24dp" />

PageFragement.kt页面片段.kt

class PageOne : Fragment() {
private var _binding: FragmentPageOneBinding? = null
private val binding get() = _binding!!
private val mainViewModel: MainViewModel by viewModels()


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = FragmentPageOneBinding.inflate(inflater, container, false)

    mainViewModel.title_1.observe(viewLifecycleOwner, Observer {
        binding.pageOneTitle1.text = it.toString()
    })        

    val view = binding.root
    val vFlipper: ViewFlipper = binding.viewFlipper as ViewFlipper

    return view
}

Is there something wrong??有什么不对??

My first guess is that you didn't set the viewmodel on the binding.我的第一个猜测是您没有在绑定上设置视图模型。 You have:你有:

app:cornerRadius="28dp"
android:onClick="@{()->mainViewModel.changeCat()}"
android:text="CAT"

Which implies you have a variable in the layout named mainViewModel .这意味着您在名为mainViewModel的布局中有一个变量。 So you should set that on the binding.所以你应该在绑定上设置它。 And since you're using LiveData you should set the LifeCycleOwner as well.而且由于您使用的是LiveData ,您还应该设置LifeCycleOwner

_binding = FragmentPageOneBinding.inflate(inflater, container, false)

// v--- MISSING THIS -------

_binding.setMainViewModel(mainViewModel) // <- Set the variable
_binding.setLifeCycleOwner(this) // <- Also need this to observe LiveData

// ^--- MISSING THIS -------

mainViewModel.title_1.observe(viewLifecycleOwner, Observer {
    binding.pageOneTitle1.text = it.toString()
})        

val view = binding.root
val vFlipper: ViewFlipper = binding.viewFlipper as ViewFlipper

return view

This is all covered in the documentation for databinding .这在databinding 的文档中都有介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin:如何在不使用 getter 和 setter 的情况下更改 ViewModel 中 MutableLiveData 的值 - Kotlin: How to change values of MutableLiveData in ViewModel without using getters and setters 如何实现 viewModel 和 Live 数据? - how to implement viewModel and Live data? 如何使用 Kotlin 延迟加载协程刷新 ViewModel 数据? - How to refresh ViewModel data using Kotlin lazy loading coroutine? 在 RecyclerView Adapter 中使用 ViewModel 的实时数据 - Using ViewModel's live data in a RecyclerView Adapter Kotlin ViewModel 字符串列表 - Kotlin ViewModel List of String Android kotlin:可变实时数据更改未使用 xml 中的数据绑定反映/更新布局 - Android kotlin : mutable live data change not reflecting/updating the layout using data binding in xml 实时数据未更新ViewModel - Live Data is not updating ViewModel 如何在不使用 kotlin ktx 的情况下创建视图模型? - How to create a viewmodel without using kotlin ktx? 使用viewmodel在SQLite中更改数据后如何获取新数据? - How to get new data after data change in SQLite using viewmodel? Kotlin:如何使用视图模型/接口将数据从片段中的 recyclerview 传递到另一个片段? - Kotlin: How to pass data from recyclerview in fragment to another fragment using viewmodel/interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM