双向绑定无法解析setter [英] Two way binding cannot resolve a setter
问题描述
我正在尝试学习数据绑定,我有一个CreditCardViewModel
类,该类在我的fragment
中进行绑定,如下所示
I am trying to learn about data binding I have my CreditCardViewModel
class which is bind in my fragment
as follows
public class CreditCardValidatorFragment extends Fragment {
private CreditCardViewModel cardViewModel;
private CcValidateFragmentBinding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.cc_validate_fragment, container, false);
return binding.getRoot();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cardViewModel = ViewModelProviders.of(this).get(CreditCardViewModel.class);
binding.setCreditCardViewModel(cardViewModel);
binding.submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(CreditCardValidatorFragmentDirections.actionMainFragmentToSecondFragment("Arvinder"));
}
});
//required to update UI with Live data
binding.setLifecycleOwner(this);
}
}
public class CreditCardViewModel extends ViewModel {
private MutableLiveData<String> firstName = new MutableLiveData<>();
private MutableLiveData<String> lastName = new MutableLiveData<>();
private MutableLiveData<Long> ccNumber = new MutableLiveData<>();
private MutableLiveData<Integer> ccType = new MutableLiveData<>();
private MutableLiveData<String> exp = new MutableLiveData<>();
private MutableLiveData<Integer> cvv = new MutableLiveData<>();
public String getFirstName() {
return firstName.getValue();
}
public String getLastName() {
return lastName.getValue();
}
public Long getCcNumber() {
return ccNumber.getValue().longValue();
}
public Integer getCcType() {
return ccType.getValue();
}
public String getExp() {
return exp.getValue();
}
public Integer getCvv() {
return cvv.getValue();
}
public void onCvvTextChanged(CharSequence text) {
cvv.setValue(Integer.valueOf(text.toString()));
}
public void onFirstNameChanged(CharSequence text) {
firstName.setValue(text.toString());
}
public void onLastNameChanged(CharSequence text) {
lastName.setValue(text.toString());
}
public void onCCNumberChanged(CharSequence text) {
ccNumber.setValue(Long.valueOf(text.toString()));
}
public void onExpChanged(CharSequence text) {
exp.setValue(text.toString());
}
public void onCCTypeChanged(CharSequence text) {
ccType.setValue(Integer.valueOf(text.toString()));
}
}
我的部分布局
<EditText
android:id="@+id/cvvEdit"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:layout_marginTop="24dp"
android:afterTextChanged="@{(text) -> creditCardViewModel.onCvvTextChanged(text)}"
android:ems="10"
android:inputType="number"
android:text="@={creditCardViewModel.cvv}"
app:layout_constraintStart_toEndOf="@+id/cvv"
app:layout_constraintTop_toBottomOf="@+id/expirationEdit" />
尝试在我的布局中设置cvv时收到以下错误:
I received the following error when trying to set the cvv in my layout :
android:text="@={creditCardViewModel.cvv}"
表达式creditCardViewModel.getCvv()
不能反转,因此不能用于双向绑定
The expression creditCardViewModel.getCvv()
cannot be inverted, so it cannot be used in a two-way binding
详细信息:双向绑定无法解析java.lang.Integer
属性cvv
Details: Two-way binding cannot resolve a setter for java.lang.Integer
property cvv
我试图在viewModel
中添加getter和setter来正确获取属性,但仍然会收到错误.
I tried to add getters and setters in the viewModel
to get property correctly but still get the error .
推荐答案
将其作为您的私有变量在ViewModel中公开
Make it your private variable to the public in your ViewModel
public MutableLiveData<String> firstName = new MutableLiveData<>();
public MutableLiveData<String> lastName = new MutableLiveData<>();
public MutableLiveData<Long> ccNumber = new MutableLiveData<>();
public MutableLiveData<Integer> ccType = new MutableLiveData<>();
public MutableLiveData<String> exp = new MutableLiveData<>();
public MutableLiveData<Integer> cvv = new MutableLiveData<>();
并且您必须将整数转换为字符串
And you have to convert your integer to string
android:text="@={String.valueOf(creditCardViewModel.cvv)}"
这篇关于双向绑定无法解析setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!