如何使用Android数据绑定库和MVVM开发应用程序?

时间:2017-02-26 09:13:21

标签: android mvvm android-databinding 2-way-object-databinding android-mvvm

我使用过Android数据绑定库,MVVM和Retrofit。

有两个字段被称为" employeeid"和#34;密码"。当我点击登录按钮时,我需要这些值。我在LoginViewModel中编写了Button事件。当我点击登录按钮时,我得到空值。我该怎么办?

如果我错误地使用数据绑定库和MVVM。你能提一下吗?

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();

    }

模特 - 登录:

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>

1 个答案:

答案 0 :(得分:0)

问题在于:

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();

}

您正在实例化新的LoginViewModel而不是使用现有的LoginViewModel。这样做:

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

或者,您可以将它们作为参数传递给绑定表达式。如果按钮事件处理程序与视图模型位于不同的类中,则此选项非常有用:

    <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" />

点击处理程序如下所示:

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