[英]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.