我是 Android 开发新手,我正在尝试通过 MVVM 架构了解实时数据。
我试图让主要活动识别出属于活动视图模型的对象何时发生变化。
我创建了一个简单的登录活动,它从用户名和密码字段中获取文本并将它们传递给视图模型的登录函数,然后该函数将数据发送到用户存储库,然后向 spring-server 发出 POST 请求正在我的电脑上运行。
如果用户名和密码正确,存储库登录函数将返回一个带有登录用户名的 MutableLiveData 对象,否则返回 null。
存储库工作正常(从服务器返回的数据是正确的)。视图模型有一个 MutableLiveData 类型的字段,需要在调用登录函数后更新它。在活动中,有一个观察者应该在loggedInUser字段(类型为MutableLiveData)字段中发生更改时收到通知,即使发生更改,观察者的onChange函数也永远不会激活。
我希望有一些代码可以帮助我更好地解释。
主要活动:
public class MainActivity extends AppCompatActivity {
public EditText usernameTxt;
public EditText passwordTxt;
public Button loginBtn;
public String loggedInuUser;
LoginViewModel loginViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameTxt = findViewById(R.id.usernameTxt);
passwordTxt = findViewById(R.id.passwordTxt);
loginBtn = findViewById(R.id.loginBtn);
loginViewModel = ViewModelProviders.of(this ).get(LoginViewModel.class);
loginViewModel.init();
try {
loginViewModel.getLoggedInUser().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
Toast toast= Toast.makeText(MainActivity.this,"changed" , Toast.LENGTH_LONG );
toast.show();
}
}
);
}catch (Exception e){
System.out.println("==========================================================");
System.out.println( e.getMessage());
System.out.println("==========================================================");
}
}
protected void onLogInCliked(View v ){
// Toast toast= Toast.makeText(getApplicationContext(),loggedInuUser, Toast.LENGTH_LONG );
// toast.show();
loginViewModel.login(usernameTxt.getText().toString(),passwordTxt.getText().toString());
// Toast toast2= Toast.makeText(getApplicationContext(),loggedInuUser, Toast.LENGTH_LONG );
// toast2.show();
}
}
视图模型:
public class LoginViewModel extends ViewModel {
private UsersRepository usersRepository;
private MutableLiveData<String> loggedInUser;
public void init(){
if(loggedInUser!= null){
return;
}
usersRepository = UsersRepository.getInstance();
loggedInUser=new MutableLiveData<>();
}
public MutableLiveData<String> getLoggedInUser(){
return loggedInUser;
}
public void login(String userName , String hashedPassword) {
loggedInUser = usersRepository.login(userName, hashedPassword);
}
}
存储库:
public class UsersRepository {
private static UsersRepository usersRepository;
public static UsersRepository getInstance(){
if (usersRepository == null){
usersRepository = new UsersRepository();
}
return usersRepository;
}
private UsersRepositoryApi usersRepositoryApi;
public UsersRepository(){
usersRepositoryApi = RetrofitService.cteateService(UsersRepositoryApi.class);
}
public MutableLiveData<String> login(String username , String hashedPassword){
final MutableLiveData<String> loggedInUser = new MutableLiveData<>();
User user = new User(username,hashedPassword);
usersRepositoryApi.login(user).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
loggedInUser.setValue(response.body());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
loggedInUser.setValue(null);
}
});
return loggedInUser;
}
}
在 mainActivity 中,我设置了观察者,我希望应用程序显示我的 Toast 消息,但什么也没有发生。
我试图看看视图模型中发生了什么,这有点奇怪,
所以我打印了这样的东西:
public void login(String userName , String hashedPassword) {
System.out.println("222======================================");
System.out.println("==========================================");
System.out.println("==========================================");
System.out.println(loggedInUser.getValue());
System.out.println("==========================================");
System.out.println("==========================================");
System.out.println("==========================================");
loggedInUser = usersRepository.login(userName, hashedPassword);
System.out.println("333======================================");
System.out.println("==========================================");
System.out.println("==========================================");
System.out.println(loggedInUser.getValue());
System.out.println("==========================================");
System.out.println("==========================================");
System.out.println("==========================================");
}
在我第一次运行登录函数时,222和333的输出都是空,但在第二次运行登录函数时,222的输出是loggedInUser,333的输出是空
在这两种情况下,观察者的 on Change 函数都未被访问
有人知道我做错了什么吗?
谢谢
罗南!