6

我正在为我的数据使用新的分页库。创建 ViewModel 并首次初始化实时数据时,一切正常。问题是当我点击菜单项并想用一组不同的数据更新它时,我无法更新我的实时数据的值。然后我的片段中的 onChanged 方法不会被调用。我已阅读有关 MutableLiveData 以及 setValue 和 postValue 等可以更新实时数据的方法,但在我的情况下,我使用的是 LivePagedListProvider 并且无法从数据库返回 MutableLiveData。

道:

@Query("SELECT * FROM teams ORDER BY name ASC")
LivePagedListProvider<Integer, Team> getAllTeams();

分段:

mTeamViewModel.mTeamsList.observe(this, new Observer<PagedList<Team>>() {
        @Override
        public void onChanged(@Nullable PagedList<Team> teams) {
            mTeamAdapter.setList(teams);
        }
});

视图模型:

public LiveData<PagedList<Team>> mTeamsList;

@Inject
DatabaseManager mDatabaseManager;

void setTeamViewModel(final DatabaseManager databaseManager) {
    mDatabaseManager = databaseManager;

    mTeamsList = mDatabaseManager.getAllTeams().create(
            0,
            new PagedList.Config.Builder()
                    .setPageSize(50)
                    .setPrefetchDistance(50)
                    .setEnablePlaceholders(true)
                    .build());

}

boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_favorite:
            mTeamsList = mDatabaseManager.getFavoriteTeams().create(
                    0,
                    new PagedList.Config.Builder()
                            .setPageSize(50)
                            .setPrefetchDistance(50)
                            .build());
            return true;
        default: return false;
    }
}
4

3 回答 3

10

Finally I found the solution after the struggle of 3 days,

For updating the value of LiveData<PagedList> we can use MediatorLiveData, as follows, inside your ViewModel class:

public LiveData<PagedList<RecipeListPojo>> liveData;
private MediatorLiveData<PagedList<RecipeListPojo>> mediatorLiveData;

public RecipeListViewModel(@NonNull Application application) {
    super(application);
    mediatorLiveData = new MediatorLiveData<>();
}

public MediatorLiveData<PagedList<RecipeListPojo>> init(RecipeDao recipeDao, RecipeFrom recipeFrom, String orderBy) {
    liveData = new LivePagedListBuilder(recipeDao.getAllRecipesList(simpleSQLiteQuery), 6).build();
    
    mediatorLiveData.addSource(liveData, new Observer<PagedList<RecipeListPojo>>() {
            @Override
            public void onChanged(@Nullable PagedList<RecipeListPojo> recipeListPojos) {
                mediatorLiveData.setValue(recipeListPojos);
            }
        });
    return mediatorLiveData;
}

Further details can be found here about MediatorLiveData

于 2018-05-08T11:36:06.993 回答
2

在模型中创建一isFavourite列。然后:

@Query("SELECT * FROM teams WHERE isFavourite = true")
LivePagedListProvider<Integer, Team> getFavouriteTeams();

Then you should have a ViewModel with a function/field that returns a MutableLiveData<PagedList<Team>> and in the Fragment/Activity you observe to that field. In your Fragment/Activity you tell the ViewModel every change you need to do to de list showed. Then in the ViewModel you assign different values to the live data list.

于 2017-11-16T19:58:34.660 回答
1

Transformations.switchMap can help: https://developer.android.com/reference/android/arch/lifecycle/Transformations#switchmap

class SearchViewModel() : ViewModel(){

val teamId = MutableLiveData<Int>()

var teamsList: LiveData<PagedList<Team>>=
    Transformations.switchMap(teamId) { id ->
        //just example
        if (id <= 0) {
            //1
            searchDbManager.searchWithoutId()
        } else {
            //2
            anotherSearchDbManager.searchWithId(id)
        }.setBoundaryCallback(SearchResultBoundaryCallback()).build()
    }
}

fun changeTeamId(id : Int){
   teamId.postValue(id)
}

If you want to change the search process to search with team ID (or switch to another datasource), just call from Fragment:

viewModel.changeTeamId(2)
于 2019-04-01T15:04:01.650 回答