Search code examples
androidmvvmandroid-databinding2-way-object-databindingandroid-mvvm

How to develop application with Android Data Binding Library and MVVM?


I have used Android Data Binding Library, MVVM and Retrofit.

There are two field is called "employeeid" and "password". When I will click on Login Button, I need these value. I have written Button event in LoginViewModel. When I click on Login Button ,I get null value. What can I do?

If I make mistake with Data Binding Library and MVVM. Could you please mention it?

public void onClickLogin(View view) {
        LoginViewModel loginViewModel=new LoginViewModel(mContext);

        String EmpId=loginViewModel.getEmployeeid();
        String Pass=loginViewModel.getPassword();


        Toast.makeText(mContext, "EmployeeID is:"+EmpId+"Password is:"+Pass, Toast.LENGTH_LONG).show();

    }

Model- Login:

public class Login {

    @SerializedName("EmpID")
    private String employeeid;
    @SerializedName("FullName")
    private String fullname;
    @SerializedName("UserType")
    private String usertype;

    String password;

    public Login(String employeeid, String fullname, String usertype, String password) {
        this.employeeid = employeeid;
        this.fullname = fullname;
        this.usertype = usertype;
        this.password = password;
    }

    public String getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(String employeeid) {
        this.employeeid = employeeid;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getUsertype() {
        return usertype;
    }

    public void setUsertype(String usertype) {
        this.usertype = usertype;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

LoginViewModel:

public class LoginViewModel extends BaseObservable {

    private Login mLogin;
    private String employeeid;
    private String password;
    private String fullname;
    private Context mContext;

    String TAG= LoginActivity.class.getSimpleName();

    public LoginViewModel(Context mContext) {
        this.mContext = mContext;
    }


    @Bindable
    public String getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(String employeeid) {
        this.employeeid = employeeid;
        notifyPropertyChanged(BR.employeeid);
    }

    @Bindable
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        notifyPropertyChanged(BR.password);
    }

    //@Bindable
    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
        //notifyPropertyChanged(BR.fullName);
    }





    public void onClickLogin(View view) {
        LoginViewModel loginViewModel=new LoginViewModel(mContext);

        String EmpId=loginViewModel.getEmployeeid();
        String Pass=loginViewModel.getPassword();


        Toast.makeText(mContext, "EmployeeID is:"+EmpId+"Password is:"+Pass, Toast.LENGTH_LONG).show();

    }

}

LoginActivity:

public class LoginActivity extends AppCompatActivity {
    Login gg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);
        ActivityLoginBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_login);

        LoginViewModel lvm=new LoginViewModel(this);
        //lvm.setFullName("Durlove Roy");
        binding.setLoginVM(lvm);
        binding.setLoginActivity(this);


    }
}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_login">

    <data>
        <variable
            name="loginVM"
            type="com.nitolniloygroup.operating.viewmodel.LoginViewModel"></variable>
        <variable
            name="loginActivity"
            type="com.nitolniloygroup.operating.view.activity.LoginActivity"></variable>
    </data>


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text="@={loginVM.employeeid}"
            android:ems="10"
            android:id="@+id/editText" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:text="@={loginVM.password}"
            android:ems="10"
            android:id="@+id/editText2" />

        <Button
            android:text="Login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{loginVM::onClickLogin}"
            android:id="@+id/button" />
    </LinearLayout>


</layout>

Solution

  • The problem is here:

    public void onClickLogin(View view) {
        LoginViewModel loginViewModel=new LoginViewModel(mContext);
    
        String EmpId=loginViewModel.getEmployeeid();
        String Pass=loginViewModel.getPassword();
    
    
        Toast.makeText(mContext, "EmployeeID is:"+EmpId+"Password is:"+Pass, Toast.LENGTH_LONG).show();
    
    }
    

    You are instantiating a new LoginViewModel instead of using the existing one. Do this instead:

    public void onClickLogin(View view) {
        Toast.makeText(mContext, "EmployeeID is:" + this.employeeid
            + " Password is:" + this.password, Toast.LENGTH_LONG).show();
    }
    

    Alternatively, you can pass them as parameters in the binding expression. This can be useful if the button event handler is in a different class than the view model:

        <Button
            android:text="Login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{() -> loginVM.onClickLogin(loginVM.employeeid, loginVM.password)}"
            android:id="@+id/button" />
    

    And the click handler would look like this:

    public void onClickLogin(String empId, String pwd) {
        Toast.makeText(mContext, "EmployeeID is:" + empId
            + " Password is:" + pwd, Toast.LENGTH_LONG).show();
    }