【发布时间】:2020-07-18 15:43:28
【问题描述】:
我试图在 android 中查看模型,所以对于 MainViewModel.java 我写了这个:
public class MainViewModel extends ViewModel {
private String textView;
private String editText;
//@Bindable
public String getTextView(){
return textView;
}
private void setTextView(String value){
textView=value;
}
//@Bindable
public TextWatcher getEditTextWatcher() {
return new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
setTextView(s.toString());
}
...
};
}
}
我在 ActivityMain.xml 中写了这个:
<TextView
android:text="View-Model / Data-Binding"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<TextView
android:id="@+id/main_text_view"
android:text="@{mainvm.textView}"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<EditText
android:id="@+id/main_edit_text"
app:textChangeListener="@{mainvm.editTextWatcher}"
android:layout_width="match_parent"
android:layout_height="40dp"/>
我收到 2 个错误:
Cannot find a setter for <android.widget.EditText app:textChangeListener> that accepts parameter type 'android.text.TextWatcher'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
还有,
error: cannot find symbol class ActivityMainBindingImpl
有些文章使用@Binable注解扩展BaseObservable,这不是ViewModel的东西。
所以我的问题是如何解决这个问题?
【问题讨论】:
-
要观察
textView的值吗? -
@Zain 不,我想在 textview 中显示 edittext 的值,使用 viewmodel
标签: java android mvvm viewmodel