如何使用视图模型而不使用数据绑定使我的编辑文本在配置更改后仍然存在

时间:2019-07-28 14:55:38

标签: android mvvm android-livedata mutablelivedata

我已经创建了一个登录屏幕。 如果我在编辑文本中输入内容并旋转设备, 数据丢失。 我如何制作我的编辑文本,以适应这些变化?

我已经创建了一个loginViewModel,其中我只是从api中获取数据。

这是我的loginActivity

public class LogInActivity extends AppCompatActivity {
    private TextInputLayout mLoginUserName, mLoginPassword;
    private LoginViewModel loginViewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        loginViewModel = new LoginViewModel(getApplication(), 
this);


        mLoginPassword = findViewById(R.id.logInPassword);
        mLoginUserName = findViewById(R.id.logInUserName);
        Button mFinalLoginButton =  
findViewById(R.id.finalLogInButton);
        TextView mForgotPassword = 
findViewById(R.id.text_view_forgot_password);
        mForgotPassword.setOnClickListener(new 
View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LogInActivity.this, 
ForgotPasswordSend.class));
            }
        });

        mFinalLoginButton.setOnClickListener(new 
View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userName = 
mLoginUserName.getEditText().getText().toString().trim();
                final String userPass = 
mLoginPassword.getEditText().getText().toString().trim();

                if (userName.isEmpty())
                    mLoginUserName.setError("Please enter User 
Name");
                else mLoginUserName.setError(null);

                if (userPass.isEmpty()) {
                    mLoginPassword.setError("Please enter 
password");
                } else {
                    mLoginPassword.setError(null);
                    loginViewModel.logInRequest(userName, 
userPass);
                }
            }


        });

    }}

这是我的loginViewModel。现在,它不能用作视图模型。我刚刚命名了它,但我想使其作为viewmodel类工作

class LoginViewModel extends AndroidViewModel {
private final Context context;
private final SharedPrefs sharedPrefs = new SharedPrefs(getApplication());


public LoginViewModel(@NonNull Application application, Context context) {
    super(application);
    this.context = context;
}

public void logInRequest(final String userName, String userPassword) {
    final JSONObject jsonObject = new JSONObject();
    try {

        jsonObject.put("identifier", userName);
        jsonObject.put("password", userPassword);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    APIService apiService = RetrofitClient.getAPIService();
    Call<String> logInResponse = apiService.logIn(jsonObject.toString());
    logInResponse.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.message().equals("OK")) {
                try {
                    JSONObject jsonObjects = new JSONObject(response.body());

                    JSONObject token = jsonObjects.getJSONObject("token");

                    String key = token.getString("token");
                    String sUserName = jsonObjects.getString("username");
                    String email = jsonObjects.getString("email");
                    String firstName = jsonObjects.getString("firstName");
                    String lastName = jsonObjects.getString("lastName");

                    sharedPrefs.saveUserName(sUserName);
                    sharedPrefs.saveEmail(email);
                    sharedPrefs.saveFullName(firstName, lastName);
                    sharedPrefs.saveToken(key);
                    context.startActivity(new Intent(context, HomeActivity.class));


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                Toast.makeText(getApplication(), "Wrong User Name or Password", Toast.LENGTH_SHORT).show();
            }


        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Toast.makeText(getApplication(), "Something went wrong 
please try again", Toast.LENGTH_SHORT).show();

            }
        });

    }}

0 个答案:

没有答案