应该返回LiveData的会议室查询始终返回NULL

时间:2020-04-02 21:05:53

标签: android sqlite android-livedata

我想使用Room / SQLLite在android中创建登录屏幕。 启动我的登录活动后,将在数据库中插入一个用户对象。

按下登录按钮时,我想通过填写的用户名查找用户。 然后,我将检查输入的密码是否正确。 基于此,我将进行另一项活动,或者显示错误消息。

但这是错误的,下一个代码总是返回NULL,所以我总是会显示错误消息。

UserWithWorkorders userWithWorkorders = ie4ViewModel.getUserWithWorkorders(username.getText().toString()).getValue();

这是UserDao中的查询。如您所见,它返回LiveDate。

 @Query("SELECT * FROM User WHERE username =:username")
 LiveData<UserWithWorkorders> getUserWithWorkorders(String username);

这是我的登录活动的onCreate():

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        IE4ViewModel ie4ViewModel = new ViewModelProvider(this).get(IE4ViewModel.class);

        User user = new User("John", "Doe", "john", "doe");
        ie4ViewModel.insertUser(user);

        final Button loginButton = findViewById(R.id.loginButton);
        final EditText username = findViewById(R.id.username);
        final EditText password = findViewById(R.id.password);
        final TextView errorMessage = findViewById(R.id.errorMessage);


        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserWithWorkorders userWithWorkorders = ie4ViewModel.getUserWithWorkorders(username.getText().toString()).getValue();

                if (userWithWorkorders != null) {
                    if(userWithWorkorders.user.getPassword().equals(password.getText().toString())){
                        Intent i = new Intent(LoginActivity.this, WorkordersActivity.class);
                        i.putExtra("userObject", userWithWorkorders);
                        startActivity(i);
                    }
                    else {
                        errorMessage.setVisibility(View.VISIBLE);
                    }
                }
                else {
                    errorMessage.setVisibility(View.VISIBLE);
                }

            }
        });

        ie4ViewModel.getUsersWithWorkorders().observe(this, new Observer<List<UserWithWorkorders>>() {
            @Override
            public void onChanged(List<UserWithWorkorders> usersWithWorkorders) {
                if (usersWithWorkorders != null && usersWithWorkorders.size() > 0) {

                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                        }
                    });
                }
            }
        });
    }

我在做什么错?如果您想查看其他代码,请告诉我。 换句话说:如果Dao查询返回的是LiveData对象,如何使用Room从SQLLite数据库中检索我的android活动中的对象?

1 个答案:

答案 0 :(得分:0)

对于这一行代码,您无需使其返回可观察的。

UserWithWorkorders userWithWorkorders = ie4ViewModel.getUserWithWorkorders(username.getText().toString()).getValue();

只需在您的viewmodel中声明一个方法。

public UserWithWorkorders getUserWithWorkorders(String value){
//get result from DAO.
}

还要使您的DAO方法返回UserWithWorkorders

现在就在您的活动中调用此方法。

 UserWithWorkorders userWithWorkorders = ie4ViewModel.getUserWithWorkorders(username.getText().toString()).getValue();

请完全确保您的数据库中有一些数据要返回。

相关问题