Using Databinding, ViewModel, LiveData (MVVM). In my Layout there's a form to add Employee Details.
layout:
<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="viewModelAddEmployee"
type="com.app.roomemployeedemo.viewmodel.AddEmployeeViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/edt_lastname"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.AddEmployeeActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:text="@{viewModelAddEmployee.firstname}"
android:id="@+id/edt_firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Jaimin"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:text="@{viewModelAddEmployee.lastname}"
android:id="@+id/tv_lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Modi"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_firstname" />
<EditText
android:text="@{viewModelAddEmployee.age}"
android:id="@+id/edt_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="25"
android:inputType="textPersonName"
android:maxLength="3"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_lastname" />
<EditText
android:text="@{viewModelAddEmployee.gender}"
android:id="@+id/edt_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Gender (M/F, m/f)"
android:inputType="textPersonName"
android:maxLength="1"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_age" />
<EditText
android:text="@{viewModelAddEmployee.salary}"
android:id="@+id/edt_salary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Salary (50000)"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_gender" />
<Button
android:onClick="@{(v) -> viewModelAddEmployee.onAddEmployeeClick(v)}"
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="40dp"
android:text="ADD NOW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_salary" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is the ViewModel class for the same :
class AddEmployeeViewModel(mContext: Context) : ViewModel(){
var mContext=mContext
lateinit var firstname:MutableLiveData<String>
lateinit var lastname:MutableLiveData<String>
lateinit var age:MutableLiveData<Int>
lateinit var gender:MutableLiveData<Char>
lateinit var salary:MutableLiveData<Int>
lateinit var repositoryEmployee:EmployeeRepository
init{
repositoryEmployee= EmployeeRepository(mContext)
}
fun onAddEmployeeClick(view: View)
{
repositoryEmployee.onAddEmployeeClick(mContext,firstname,lastname,age,gender,salary)
}
}
Initialized Viewmodel and binding utility in Activity class as below :
class AddEmployeeActivity : AppCompatActivity() {
lateinit var viewModelAddEmployee: AddEmployeeViewModel
lateinit var binding: ActivityAddEmployeeBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModelAddEmployee = ViewModelProvider(this,AddEmployeeFactory(this@AddEmployeeActivity)).get(AddEmployeeViewModel::class.java)
binding = DataBindingUtil.setContentView(
this@AddEmployeeActivity,
R.layout.activity_add_employee
)
binding?.setLifecycleOwner(this)
binding?.viewModelAddEmployee = viewModelAddEmployee
}
}
But, Getting below Error :
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property gender has not been initialized
at com.app.roomemployeedemo.viewmodel.AddEmployeeViewModel.getGender(AddEmployeeViewModel.kt:16)
What might be the issue? Especially with Char type gender!!
In kotlin you must initialize lateinit property. Add below line to oncreate -
gender = MutableLiveData<Char>()