从xml发送数据对象到viewmodel。

问题描述 投票:0回答:1

当我试图这样做时,我发现null对象!我如何能发送数据对象本身到视图模型?

XML :

  <variable
        name="signUpObject"
        type="com.rabe7.community.model.request.register.RegisterRequest" />


    <variable
        name="signUpClickListener"
        type="com.rabe7.community.view_model.user_management.RegisterViewModel" />


      <androidx.cardview.widget.CardView
                    android:id="@+id/cv_sign_up_submit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/dp8w"
                    android:onClick="@{() -> signUpClickListener.onRegisterSubmitClicked(signUpObject)}"
                    card_view:cardElevation="@dimen/dp8w">

RegisterViewModel.为什么这个对象是空的?

   public void onRegisterSubmitClicked(RegisterRequest registerRequest){

}

为什么这个对象是空的? 我想把所有的数据绑定在xml上,然后把完整的数据对象发送给viewmodel来使用它。

XML:

         <EditText
                        android:id="@+id/et_sign_up_email"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:hint="@string/label_email"
                        android:inputType="textEmailAddress"
                        android:maxLines="1"
                        android:text="@={signUpObject.userEmail}" />

   <EditText
                        android:id="@+id/et_sign_up_password"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:hint="@string/label_sign_up_password"
                        android:imeActionLabel="SignIn"
                        android:imeOptions="actionUnspecified"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:text="@={signUpObject.userPassword}" />

所以用户输入他的电子邮件和电话,然后点击提交...并且持有电子邮件和电话的对象传递给viewmdol。

这就是数据类

public class RegisterRequest extends BaseObservable {


@SerializedName("Email")
private String userEmail;
@SerializedName("Password")
private String userPassword;
@Expose
private String userConfirmPassword;
@SerializedName("Phone")
private String userPhone;
@SerializedName("ImagePath")
private String imagePath;
@SerializedName("Name")
private String userName;

//getters

@Bindable
public String getUserEmail() { return userEmail; }
@Bindable
public String getUserPassword() { return userPassword; }
@Bindable
public String getUserConfirmPassword(){return userConfirmPassword;}
@Bindable
public String getUserPhone() { return userPhone; }
@Bindable
public String getImagePath() { return imagePath; }
@Bindable
public String getUserName() { return userName; }

//setters

public void setUserEmail(String userEmail) {
    this.userEmail = userEmail;
    notifyPropertyChanged(BR.userEmail);
}
public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
    notifyPropertyChanged(BR.userPassword);
}
public void setUserConfirmPassword(String userConfirmPassword) {
    this.userConfirmPassword = userConfirmPassword;
    notifyPropertyChanged(BR.userConfirmPassword);
}
public void setUserPhone(String userPhone) {
    this.userPhone = userPhone;
    notifyPropertyChanged(BR.userPhone);
}
public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
    notifyPropertyChanged(BR.imagePath);
}
public void setUserName(String userName) {
    this.userName = userName;
    notifyPropertyChanged(BR.userName);
}

}

恕我英语不好

java android xml mvvm
1个回答
0
投票

你在你的activityfragment中设置了这些变量吗?我想指出,这些对象就像变量一样,默认情况下没有值。首先,我们在activityfragment中初始化数据绑定对象,然后调用binding.setSignUpObject()和binding.setSignUpClickListener()来设置这些对象。

如果您已经在活动中调用了这些函数,请在您初始化这些函数的地方添加相关代码。

© www.soinside.com 2019 - 2024. All rights reserved.