无法使用新值更新LiveData

时间:2018-07-28 22:42:11

标签: android android-room android-livedata

我正在尝试更新LiveData列表,但无法执行。 UI显示数据库中的相同值。 因此,在 MainActivity 中,有一个函数可以从数据库加载数据并为其设置观察者(如果数据已更改)。活动中有一个带有侦听器的按钮,当我轻按该按钮时(将getData();放在此处)。我希望,当我点击按钮时,它会从数据库中加载新的随机值,但实际上它始终显示相同的值。您能给我提意见还是帮助我如何解决这一难题?我预计这是一个琐碎而愚蠢的错误(或我的无知)。感谢您的关注。

@OnClick(R.id.button)
void submitButton(View view) {
        getData();
}

private void getData(){

User userList = new User();

UserViewModel mUserViewModel;

mUserViewModel = ViewModelProviders.of(this).get(UserViewModel.class);

mUserViewModel.getRandom().observe(this, new Observer<List<User>>() {
    @Override
    public void onChanged(@Nullable final List<User> user) {
        // Update UI
        if (user != null) {
            User userList = user.get(0);
            updateUI(userList);
        }
    }
});
}

UserDao.kt

@Dao
public interface UserDao {
    @Query("SELECT * FROM user ORDER BY RANDOM() LIMIT 1")
    fun getRandom(): LiveData<List<User>>
}

UserRepository.java

public class UserRepository {

    private UserDao mUserDao;
    private LiveData<List<User>> mUser;

    public UserRepository(Application application) {
        AppDatabase db = AppDatabase.getDatabase(application);
        mUserDao = db.userDao();
        mUser = mUserDao.getRandom();
    }

    public LiveData<List<User>> getRandom() {
        return mUser;
    }
}

UserViewModel

public class UserViewModel extends AndroidViewModel {

    private UserRepository mRepository;
    private LiveData<List<User>> mUser;

    public UserViewModel(Application application){
        super(application);
        mRepository = new UserRepository(application);
        mUser = mRepository.getRandom();
    }

    public LiveData<List<User>> getRandom() {
        return mUser;
    }
}

1 个答案:

答案 0 :(得分:0)

您可能要使用MediatorLiveData而不是直接公开LiveData引用。要刷新随机用户,您只需要重新查询DAO,它将更新Activity中的值。这可以通过如下所述进行一些更改来完成。

活动:

public class MainActivity extends AppCompatActivity {

    private final UserViewModel mUserViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Other onCreate code 
        mUserViewModel = ViewModelProviders.of(this).get(UserViewModel.class);

        mUserViewModel.getRandom().observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable final User user) {
                // Update UI
                if (user != null) {
                    updateUI(user);
                }
            }
        });
    }

    @OnClick(R.id.button)
    void submitButton(View view) {
        mUserViewModel.refresh();
    }
}

道:

@Dao
public interface UserDao {
    @Query("SELECT * FROM user ORDER BY RANDOM() LIMIT 1")
    fun getRandom(): LiveData<User>
}

存储库:

public class UserRepository {

    private final UserDao mUserDao;

    public UserRepository(Application application) {
        AppDatabase db = AppDatabase.getDatabase(application);
        mUserDao = db.userDao();
    }

    public LiveData<User> getRandom() {
        return mUserDao.getRandom();
    }
}

ViewModel:

public class UserViewModel extends AndroidViewModel {

    private final UserRepository mRepository;
    private final MediatorLiveData<User> mRandomUserMediator = new MediatorLiveData<>();

    public UserViewModel(Application application){
        super(application);
        mRepository = new UserRepository(application);
        mRandomUserMediator.addSource(mRepository.getRandom(), mRandomUserMediator::setValue);
    }

    public LiveData<User> getRandom() {
        return mRandomUserMediator;
    }

    public void refresh() {
        mRepository.getRandom();
    }
}