简体   繁体   English

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

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

I have used Android Data Binding Library, MVVM and Retrofit. 我已经使用了Android数据绑定库,MVVM和翻新版。

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. 我在LoginViewModel中编写了Button事件。 When I click on Login Button ,I get null value. 当我单击登录按钮时,我得到空值。 What can I do? 我能做什么?

If I make mistake with Data Binding Library and MVVM. 如果我对数据绑定库和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: 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: 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 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>

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. 您将实例化一个新的LoginViewModel而不是使用现有的LoginViewModel。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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