如何解决ViewModel /存储库显示为空白
我正在遵循Github Browser app tutorial来构建我的项目而不使用数据绑定。显示只是空白,仅显示导航视图。我无法弄清楚哪里出了问题。我尝试调试,所有显示的结果都是null。没有错误。
ViewModel.java:
env NODE_ENV=test jest
Repository.java:
public class ListViewModel extends ViewModel {
private final LiveData<Resource<List<Something>>> repositories;
private int langID;
private int categoryID;
private MutableLiveData<List<Something>> networkInfoObservable = new MutableLiveData<>();
private static String TAG = "SomethingViewModel";
@SuppressWarnings("unchecked")
@Inject
public ListViewModel(ListRepository listRepository) {
repositories = Transformations.switchMap(networkInfoObservable,someList ->
listRepository.loadList(2,1)); // hard-coding values to test)
}
@VisibleForTesting
public void setListArgs(int langID,int categoryID) {
this.langID = langID;
this.categoryID = categoryID;
}
@VisibleForTesting
public LiveData<Resource<List<Something>>> getList() {
return repositories;
}
}
DAO.java:
@Singleton
public class ListRepository {
private final SomeDao someDao;
private final SomeService someService;
private final AppExecutors appExecutors;
private static String TAG = "LIST_REPOSITORY";
@Inject
ListRepository(AppExecutors appExecutors,SomeDao someDao,SomeService someService) {
this.someDao = someDao;
this.someService = someService;
this.appExecutors = appExecutors;
}
public LiveData<Resource<List<Something>>> loadList(int langId,int categoryID) {
return new NetworkBoundResource<List<Something>,ListWrapper<Something>>(appExecutors) {
@Override
protected void saveCallResult(@NonNull ListWrapper<Something> item) {
for (Something someItem : item.someItems){
someItem.lang_id = langId;
someItem.cat_id = categoryID;
someDao.insertList(someItem);
}
}
@Override
protected boolean shouldFetch(@Nullable List<Something> data) {
return data == null;
}
@NonNull
@Override
protected LiveData<List<Something>> loadFromDb() {
return someDao.getList(langId,categoryID);
}
@NonNull
@Override
protected LiveData<ApiResponse<ListWrapper<Something>>> createCall() {
Log.d(TAG,"Test");
return someService.getList(langId,categoryID);
}
}.asLiveData();
}
}
APIService.java:
@Dao
public interface SomeDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
public void insertList(Something someList);
@Query("SELECT * FROM list WHERE langId=:langId AND catId=:catId")
public LiveData<List<Something>> getList(int langId,int catId);
}
BaseFragment:
public interface SomeService {
@POST("/getCategoryItem")
LiveData<ApiResponse<ListWrapper<Something>>> getList(@Query("langId") Integer langId,@Query("catId") Integer category);
}
解决方法
我做了一些研究,发现必须实现一个内部静态类来更新参数:
public class ListViewModel extends ViewModel {
private final LiveData<Resource<List<Something>>> repositories;
private final MutableLiveData<ListArgs> listArgs;
private static String TAG = "SomethingViewModel";
@SuppressWarnings("unchecked")
@Inject
public ListViewModel(ListRepository listRepository) {
this.listArgs = new MutableLiveData<>();
repositories = Transformations.switchMap(listArgs,listArg ->
listRepository.loadList(listArg.langID,listArg.categoryID));
}
@VisibleForTesting
public void setListArgs(int langID,int categoryID) {
ListArgs update = new ListArgs(langID,categoryID);
if (Objects.equals(listArgs.getValue(),update)) {
return;
}
listArgs.setValue(update);
}
@VisibleForTesting
public LiveData<Resource<List<Something>>> getList() {
return repositories;
}
static class ListArgs {
final int langID;
final int categoryID;
ListArgs(int langID,int categoryID) {
this.langID = langID;
this.categoryID = categoryID;
}
}
}
,
您的代码是在不创建实例的抽象类内部实现的,因此尚未被调用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。