为什么我无法从视图模型 class 中获取值?
Why can't I get the value from the view model class?
我想使用 mvvm 设计模式进行数据绑定。但是,我无法将数据导入我的视图模型 class 结果 returns null。最后,当我对 activity 执行相同操作时,我成功传输了数据,但是当我将其与预告片一起使用时,我遇到了问题。
片段class
class AddTenant : Fragment() {
private lateinit var viewModel : AddTenantVM
private lateinit var dataBinding : FragmentAddTenantBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
dataBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_add_tenant,container,false)
return dataBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this).get(AddTenantVM::class.java)
dataBinding.addTenant=viewModel
dataBinding.lifecycleOwner=this
button.setOnClickListener {
viewModel.tenant()
}
}
}
XML class
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="addTenant"
type="com.example.kiratakip.viewmodel.AddTenantVM" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddTenant">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="Adı Soyadı"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@{addTenant.name}"/>
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="T.C"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
android:text="@{addTenant.city}"
/>
<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="Telefon"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName2"
android:text="@{addTenant.tc}"
/>
<EditText
android:id="@+id/editTextTextPersonName4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="İl"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2"
android:text="@{addTenant.telefon}"/>
<EditText
android:id="@+id/editTextTextPersonName5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="İlçe"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName4"
android:text="@{addTenant.district}"/>
<EditText
android:id="@+id/editTextTextPersonName6"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Adres Detay"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName5"
android:text="@{addTenant.adress}"/>
<EditText
android:id="@+id/editTextTextPersonName7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="Kontrat Baslangıç"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName6"
android:text="@{addTenant.contractStart}"/>
<EditText
android:id="@+id/editTextTextPersonName8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="Kontrart Bitiş"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName7"
android:text="@{addTenant.contractFinish}"/>
<EditText
android:id="@+id/editTextTextPersonName9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="Kira Tutarı"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName7"
/>
<EditText
android:id="@+id/editTextTextPersonName10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="Depozito"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName9"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="59dp"
android:text="Oluştur"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName10"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
视图模型class
class AddTenantVM(application: Application): BaseViewModel(application) {
val addTenant = MutableLiveData<HomeList>()
val name = MutableLiveData<String>()
val tc = MutableLiveData<String>()
val telefon = MutableLiveData<String>()
val city = MutableLiveData<String>()
val district = MutableLiveData<String>()
val adress = MutableLiveData<String>()
val contractStart = MutableLiveData<String>()
val contractFinish = MutableLiveData<String>()
fun tenant (){
println(name.value.toString())
}
}
尝试在 return dataBinding.root
之前初始化视图模型
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
dataBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_add_tenant,container,false)
viewModel = ViewModelProviders.of(this).get(AddTenantVM::class.java)
dataBinding.addTenant=viewModel
dataBinding.lifecycleOwner=this
return dataBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.setOnClickListener {
viewModel.tenant()
}
}
我想使用 mvvm 设计模式进行数据绑定。但是,我无法将数据导入我的视图模型 class 结果 returns null。最后,当我对 activity 执行相同操作时,我成功传输了数据,但是当我将其与预告片一起使用时,我遇到了问题。
片段class
class AddTenant : Fragment() {
private lateinit var viewModel : AddTenantVM
private lateinit var dataBinding : FragmentAddTenantBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
dataBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_add_tenant,container,false)
return dataBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this).get(AddTenantVM::class.java)
dataBinding.addTenant=viewModel
dataBinding.lifecycleOwner=this
button.setOnClickListener {
viewModel.tenant()
}
}
}
XML class
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="addTenant"
type="com.example.kiratakip.viewmodel.AddTenantVM" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddTenant">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="Adı Soyadı"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@{addTenant.name}"/>
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="T.C"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
android:text="@{addTenant.city}"
/>
<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="Telefon"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName2"
android:text="@{addTenant.tc}"
/>
<EditText
android:id="@+id/editTextTextPersonName4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="İl"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2"
android:text="@{addTenant.telefon}"/>
<EditText
android:id="@+id/editTextTextPersonName5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="İlçe"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName4"
android:text="@{addTenant.district}"/>
<EditText
android:id="@+id/editTextTextPersonName6"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Adres Detay"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName5"
android:text="@{addTenant.adress}"/>
<EditText
android:id="@+id/editTextTextPersonName7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="Kontrat Baslangıç"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName6"
android:text="@{addTenant.contractStart}"/>
<EditText
android:id="@+id/editTextTextPersonName8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="Kontrart Bitiş"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName7"
android:text="@{addTenant.contractFinish}"/>
<EditText
android:id="@+id/editTextTextPersonName9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="Kira Tutarı"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName7"
/>
<EditText
android:id="@+id/editTextTextPersonName10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:ems="10"
android:hint="Depozito"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName9"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="59dp"
android:text="Oluştur"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName10"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
视图模型class
class AddTenantVM(application: Application): BaseViewModel(application) {
val addTenant = MutableLiveData<HomeList>()
val name = MutableLiveData<String>()
val tc = MutableLiveData<String>()
val telefon = MutableLiveData<String>()
val city = MutableLiveData<String>()
val district = MutableLiveData<String>()
val adress = MutableLiveData<String>()
val contractStart = MutableLiveData<String>()
val contractFinish = MutableLiveData<String>()
fun tenant (){
println(name.value.toString())
}
}
尝试在 return dataBinding.root
之前初始化视图模型override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
dataBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_add_tenant,container,false)
viewModel = ViewModelProviders.of(this).get(AddTenantVM::class.java)
dataBinding.addTenant=viewModel
dataBinding.lifecycleOwner=this
return dataBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.setOnClickListener {
viewModel.tenant()
}
}