I start to learn MVVM and I started with the below link
I've written everything that the site explained but when I run the program, I have the below error.
[databinding] {"msg":"Cannot find a setter for \u003candroid.widget.Button bind:toastMessage\u003e that accepts parameter type \u0027java.lang.String\u0027\n\nIf a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.","file":"app\\src\\main\\res\\layout\\login.xml","pos":[{"line0":45,"col0":37,"line1":45,"col1":57}]}
I don't know where is my error and how I can solve it. I've searched a lot and I did clean and rebuild again or I catch and restart android studio but it didn't work. If you can please help me. These are my codes:
User:
public class User {
private String emial;
private String password;
public User(String emial, String password) {
this.emial = emial;
this.password = password;
}
public void setEmial(String emial) {
this.emial = emial;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmial() {
return emial;
}
public String getPassword() {
return password;
}
}
LoginViewModel:
public class LoginViewModel extends BaseObservable {
private User user;
private String successMessage="Login was successful";
private String errorMessage="Email or Password not valid";
@Bindable
private String toastMessge=null;
public String getToastMessge(){
return toastMessge;
}
private void setToastMessge(String toastMessge){
this.toastMessge=toastMessge;
notifyPropertyChanged(BR.toastMessge);
}
public void setUserEmail(String email){
user.setEmial(email);
notifyPropertyChanged(BR.userEmail);
}
@Bindable
public String getUserEmail(){
return user.getEmial();
}
@Bindable
public String getUserPassword(){
return user.getPassword();
}
public void setUserPassword(String password){
user.setPassword(password);
notifyPropertyChanged(BR.userPassword);
}
public LoginViewModel(){
user=new User("","");
}
public void onLoginClicked(){
if(isInputDataValid()){
setToastMessge(successMessage);
}else
setToastMessge(errorMessage);
}
public boolean isInputDataValid(){
return !TextUtils.isEmpty(getUserEmail())&& Patterns.EMAIL_ADDRESS.matcher(getUserEmail()).matches()&&getUserPassword().length()>5;
}
}
Login:
public class Login extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoginBinding activityMainBinding= DataBindingUtil.setContentView(this, R.layout.login);
}
}
login.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.kit.mvvmtest.ViewModel.LoginViewModel" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="@+id/inEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="8dp"
android:text="@={viewModel.userEmail}" />
<EditText
android:id="@+id/inPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:text="@={viewModel.userPassword}" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="@{()-> viewModel.onLoginClicked()}"
android:text="LOGIN"
bind:toastMessage="@{viewModel.toastMessge}" />
</LinearLayout>
</ScrollView>