0

我创建了一个登录屏幕。如果我在编辑文本上键入内容并旋转我的设备,数据就会丢失。我怎样才能让我的编辑文本,在这些变化中幸存下来?

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

这是我的登录活动

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);
                }
            }


        });

    }}

这是我的登录视图模型。现在它不能作为视图模型工作。我刚刚命名了它,但我想让它作为一个视图模型类工作

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();

            }
        });

    }}
4

0 回答 0