mercredi 9 octobre 2019

Error cannot find symbol class ViewModel while using MVVM design pattern in android

I have just created a demo project for learning and the MVVM and how to use the Mvvm in our project. but I have found an error while running the project

error: cannot find symbol class ViewModel
error: package ViewModel does not exist
error: package ViewModel does not exist
error: package ViewModel does not exist

and here is my code

public class User extends BaseObservable {

String email,password;
boolean isDataValidate;

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public boolean isDataValidate() {
    return !TextUtils.isEmpty(getEmail())&& Patterns.EMAIL_ADDRESS.matcher(getEmail()).matches()&&
             getPassword().length()>6;
}

public void setDataValidate(boolean dataValidate) {
    isDataValidate = dataValidate;
}

}

and Here is my ViewModel class

public class LoginViewModel extends ViewModel {

private User user;
private LoginResultCallback loginResultCallback;

public LoginViewModel(LoginResultCallback loginResultCallback){
    this.loginResultCallback=loginResultCallback;
    this.user=new User();

}
public String getEmailText1(){

    return user.getEmail();
}
public String getPasswordText1(){

    return user.getPassword();
}
public TextWatcher getEmailText(){
    return new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            user.setEmail(editable.toString());


        }
    };
}

public TextWatcher getPasswordText(){

    return new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
        user.setPassword(editable.toString());
        }
    };

}


public void onLoginClicked(View view)
{

    if (user.isDataValidate()){
        loginResultCallback.onSuccess("Login was Successfull");
    }
    else{
        loginResultCallback.onError("Login Invalid Credential");
    }

}

}

and here is my MainActivity

public class MainActivity extends AppCompatActivity implements LoginResultCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityMainBinding activityMainBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
    LoginViewModel loginViewModel=new LoginViewModel(this);
    activityMainBinding.setViewModel(loginViewModel);

    //activityMainBinding.setViewModel(ViewModelProviders.of(this,new LoginViewModelFactory(this)).get(LoginViewModel.class));
}

@Override
public void onSuccess(String Message) {
    Toast.makeText(this, ""+Message, Toast.LENGTH_SHORT).show();
}

@Override
public void onError(String Error) {
    Toast.makeText(this, ""+Error, Toast.LENGTH_SHORT).show();
}

}

and here is viewModelFactory class

public class LoginViewModelFactory  extends ViewModelProvider.NewInstanceFactory {

private LoginResultCallback loginResultCallback;

public LoginViewModelFactory(LoginResultCallback loginResultCallback)
{
    this.loginResultCallback=loginResultCallback;

}

@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new LoginViewModel(loginResultCallback);
}

}

and XML is here

<?xml version="1.0" encoding="utf-8"?>
<data>

    <variable
        name="viewModel"
        type="com.example.designinfpattern.ViewModel.LoginViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:addTextChangedListener="@{viewModel.emailText}"
            android:hint="Enter Your account Email or Username"
            />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Your account password"
            app:addTextChangedListener="@{viewModel.passwordText}" />

    </LinearLayout>

    <Button
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{viewModel::onLoginClicked}"
        android:text="Login" />
</LinearLayout>

I don't know how to solve this error and I'm doing R&D but not find a proper solution. Please help me out to solve the problem

thanks in advance

1 commentaire: