我正在尝试使用Mediator Live数据来合并两个Livedata。用ViewModel和Fragment编写的方法如下所示。我收到“类错误”强制转换异常错误(详细信息在下面列出)。您能否建议出问题了。可能我认为这可能是MediatorLive从两个实时数据中获取值的方式,但我无法弄清楚,因此无法向编码人员提出要求。。请提前告知并感谢您的帮助。
// LiveData -1
public LiveData<GrpMsgTimestamp> getlastRec(Long lastbookID, String groupID)
{
return bookRepository.getlatest(lastbookID,groupID);
}
// LiveData -2
public LiveData<Integer> getNoOfUnreadBook(Long lastbookID, String groupID)//Final to be kept
{
return bookRepository.getNoOfUnreadBook(lasbookID,groupID);
}
//调解方法
MediatorLiveData liveDataMerger = new MediatorLiveData<GrpIDandLatestMsg>();
public MediatorLiveData getLiveDataMerger(Long lastmsgID,String groupID) {
liveDataMerger.addSource(getlastRec(lastbookID,groupID),value->liveDataMerger.setValue(getlastRec(lastbookID,groupID)));
liveDataMerger.addSource(getNoOfUnreadBook(lastgroupID,groupID), value -> liveDataMerger.setValue(getNoOfUnreadBook(lastbookID,groupID)));
return liveDataMerger;
}
//片段中的观察者
mViewModel.getLiveDataMerger(chatList.getLastbookID(),chatList.getGroupID()).observe(getActivity(), new Observer<GrpIDandLatestBook>() {
@Override
public void onChanged(@Nullable final GrpIDandLatestBook newName) {
// Update the UI, in this case, a TextView.
if(newName != null) {
chatList.setLast_msg(newName.getBook());
chatList.setLasttimestamp(String.valueOf(newName.getTimestamp()));
chatList.setNoofunreadBook(String.valueOf(newName.getMsgcount()));
}
}
错误:-
java.lang.ClassCastException: android.arch.lifecycle.ComputableLiveData$1 cannot be cast to com.support.android.designlibdemo.database.inernal.ModelClass.GrpIDandLatestMsg
at com.support.android.designlibdemo.View.UI.FragmentChatList$1$1.onChanged(FragmentChatList.java:61)
at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at com.support.android.designlibdemo.ViewModel.ChatListViewModel.lambda$getLiveDataMerger$1$ChatListViewModel(ChatListViewModel.java:65)
Thanks
答案 0 :(得分:0)
这是我创建的用于融合2个不同祖先列表的类。
class MelterFavorites extends MediatorLiveData<List<FavoriteEntity>> {
// ...
private List<FavoriteEntity> tmpFavoriteCities = null;
private List<FavoriteEntity> tmpFavoriteSellPoints = null;
private final Observer<List<FavoriteCity>> cityListObserver = new Observer<List<FavoriteCity>>() {
@Override
public void onChanged(final List<FavoriteCity> favoriteCityList) {
// ...
tmpFavoriteCities.clear();
// ...
if (favoriteCityList != null) {
tmpFavoriteCities.addAll(favoriteCityList);
}
// melt results
final List<FavoriteEntity> meltedFavorites = meltFavorites(tmpFavoriteCities, tmpFavoriteSellPoints);
// ...
setValue(meltedFavorites);
}
};
private final Observer<List<FavoriteSellPoint>> sellPointListObserver = new Observer<List<FavoriteSellPoint>>() {
@Override
public void onChanged(final List<FavoriteSellPoint> favoriteSellPointList) {
// ...
tmpFavoriteSellPoints.clear();
// ...
if (favoriteSellPointList != null) {
tmpFavoriteSellPoints.addAll(favoriteSellPointList);
}
// melt results
final List<FavoriteEntity> meltedFavorites = meltFavorites(tmpFavoriteCities, tmpFavoriteSellPoints);
// ...
setValue(meltedFavorites);
}
};
private List<FavoriteEntity> meltFavorites(final List<FavoriteEntity> favoriteCities, final List<FavoriteEntity> favoriteSellPoints) {
final TreeSet<FavoriteEntity> allFavorites = new TreeSet<>();
allFavorites.addAll(favoriteCities);
allFavorites.addAll(favoriteSellPoints);
return new ArrayList<>(allFavorites);
}
public MelterFavorites(final LiveData<List<FavoriteCity>> favoriteCities, LiveData<List<FavoriteSellPoint>> favoriteSellPoints) {
// build internal temporary list
tmpFavoriteCities = new ArrayList<FavoriteEntity>();
tmpFavoriteSellPoints = new ArrayList<FavoriteEntity>();
addSource(favoriteCities, cityListObserver);
addSource(favoriteSellPoints, sellPointListObserver);
}
}