Привет, друзья! У меня есть абстрактный ViewModle EmployeeViewModel
Мне нужно повторно использовать EmployeeViewModel
, и вот он
abstract class EmployeeViewModel : ViewModel() {
/** Form fields */
var name = MutableLiveData("")
var username = MutableLiveData("")
var password = MutableLiveData("")
var conformPassword = MutableLiveData("")
var mobile = MutableLiveData("")
var designation = MutableLiveData("")
var employee: Employee? = null
}
Теперь я создал дочернюю ViewModel с именем AddEmployeeViewModel
, которая используется для добавления сотрудник, и здесь
class AddEmployeeViewModel : EmployeeViewModel() {
fun onClickAddEmployee(view: View){
when{
name.value.isNullOrEmpty() -> view.context.toast("Please enter name")
mobile.value.isNullOrEmpty() -> view.context.toast("Please enter mobile number")
mobile.value!!.length != 10 -> view.context.toast(
"Mobile number should contain 10 digits"
)
designation.value == "--- Choose ---" -> view.context.toast("Please select designation")
password.value.isNullOrEmpty() -> view.context.toast("Please enter password")
conformPassword.value != password.value -> view.context.toast("Password did not match")
else ->{
}
}
}
}
при вызове onClickAddEmployee(view: View)
я вижу, что все поля пусты, но я связал все поля с этим макетом
<?xml version="1.0" encoding="utf-8"?>
<layout
tools:ignore="ContentDescription,HardcodedText,PrivateResource,RtlHardcoded"
xmlns:tools="http://schemas.android.com/tools">
<!--region Data -->
<data>
<variable
name="viewModel"
type="com.aseemsalim.mvvmtestapp.ui.employee.EmployeeViewModel" />
</data>
<!--endregion -->
<!--region Form -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--region Name-->
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutStyle">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={viewModel.name}"
android:hint="Name" />
</com.google.android.material.textfield.TextInputLayout>
<!--endregion-->
<!--region Mobile-->
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutStyle">
<com.google.android.material.textfield.TextInputEditText
android:text="@={viewModel.mobile}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Mobile"/>
</com.google.android.material.textfield.TextInputLayout>
<!--endregion-->
<!--region Designation-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
selectedItem="@={viewModel.designation}"
android:background="@drawable/spinner_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:entries="@array/designations"/>
<TextView
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:background="@android:color/background_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation"
android:textColor="@color/mtrl_outlined_stroke_color"
android:layout_marginLeft="16dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!--endregion-->
<!--region Username-->
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutStyle">
<com.google.android.material.textfield.TextInputEditText
android:text="@={viewModel.username}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"/>
</com.google.android.material.textfield.TextInputLayout>
<!--endregion-->
<!--region Password-->
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutStyle">
<com.google.android.material.textfield.TextInputEditText
android:text="@={viewModel.password}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"/>
</com.google.android.material.textfield.TextInputLayout>
<!--endregion-->
<!--region Conform Password-->
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutStyle">
<com.google.android.material.textfield.TextInputEditText
android:text="@={viewModel.conformPassword}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Conform Password"/>
</com.google.android.material.textfield.TextInputLayout>
<!--endregion-->
</LinearLayout>
<!--endregion -->
</layout>
и здесь мой код компоновки ActivityAddEmployee
Я включил код сотрудника здесь
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="com.aseemsalim.mvvmtestapp.ui.hr.AddEmployeeViewModel" />
</data>
<LinearLayout
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"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
tools:context=".ui.hr.AddEmployee">
<include layout="@layout/employee"/>
<Button
android:onClick="@{viewModel::onClickAddEmployee}"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:text="ADD EMPLOYEE"/>
</LinearLayout>
</layout>
Пожалуйста, помогите мне