简体   繁体   中英

Abstract ViewModel Not Updating LiveData Android MVVM

Hello Friends I have a abstract ViewModle EmployeeViewModel I have to reuse EmployeeViewModel and here it is

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
}

Now I have created a child ViewModel called AddEmployeeViewModel this is used for adding the employee and here it is

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 ->{

        }
    }
}
}

when onClickAddEmployee(view: View) is called i can see that all the fields are empty but I have binded all the fields with this layout

<?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>

and here is my ActivityAddEmployee layout code I have included the employee code here

<?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>

Please help me

The issue here is that your viewModel is not passing from your parent layout to the included layout. Try adding something like that:

<include layout="@layout/employee"
viewModel="@{viewModel}"/>

This should fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM