0

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

有两个字段称为“employeeid”和“password”。当我单击登录按钮时,我需要这些值。我在 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;
    }

}

登录视图模型:

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

    }

}

登录活动:

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


    }
}

活动登录.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>
4

1 回答 1

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 而不是使用现有的。改为这样做:

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();
}
于 2017-02-27T01:16:50.883 回答