ViewModel/Repository 显示空白

ViewModel/Repository displaying blank

我正在按照 Github Browser app tutorial 构建我的项目而不使用数据绑定。显示屏只是空白,只显示导航视图。我无法弄清楚我哪里出错了。我尝试调试,它的所有显示都是结果为空。没有错误。

ViewModel.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;
    }

}

Repository.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();
    }
}

DAO.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);
}

APIService.java:

public interface SomeService {

        @POST("/getCategoryItem")
        LiveData<ApiResponse<ListWrapper<Something>>> getList(@Query("langId") Integer langId,
                                                                                     @Query("catId") Integer category);

}

基础片段:

    public abstract class BaseFragment<B extends ViewBinding> extends Fragment implements Injectable {
    
    //variables
private ListViewModel listViewModel;
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            binding = onCreateBinding(inflater, container, savedInstanceState);
    
            context = this.getActivity().getApplicationContext();
    
            adapter = new ListViewAdapter();
    
            return binding.getRoot();
        }
    
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            listViewModel = new ViewModelProvider(this, viewModelFactory).get(ListViewModel.class);
            listViewModel.setListArgs(langId, categoryID);
            observeListViewModel();
        }
    
        private void observeListViewModel() {
            listViewModel.getList().observe(getViewLifecycleOwner(), result -> {
                if (result != null) {
    
                    adapter.setList(result.data);
                    adapter.setOnItemClickListener(this::itemClicked);
                }
            });
        }
    
    
    }

你的代码是在抽象中实现的 class 你没有创建实例所以它没有被调用

我做了一些研究,发现我必须实现一个内部静态 class 来更新参数:

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;
        }
    }



}