数据未通过 ViewModel 显示

Data not displaying through ViewModel

我是 android 开发的新手,无法理解为什么不显示数据。最初列表是空的,所以它应该显示空文本视图,但它也没有显示。

这是代码
查看模型工厂

public class ViewModelFactoryListItFragment implements ViewModelProvider.Factory {

    String category;
    Application application;

    public ViewModelFactoryListItFragment(Application application,String category ) {
        this.category = category;
        this.application = application;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> aClass) {
        if (aClass.isAssignableFrom(ViewModelListItFragment.class)) {
            return (T) new ViewModelListItFragment(application, category);
        } else throw new IllegalArgumentException("Viewmodel not found");
    }
}

ViewModel

public class ViewModelListItFragment extends ViewModel {

    private AppDatabase mDb;
    private LiveData<List<ListItEntry>> myEntries;
    private Application mApplication;
    private String mCategory;

    public ViewModelListItFragment(@NonNull Application application, String category) {

        this.mCategory = category;
        this.mApplication = application;
        mDb = AppDatabase.getInstance(mApplication);
        myEntries = mDb.listItDao().getListItsByCategory(mCategory);


    }

    public LiveData<List<ListItEntry>> getMyEntries() {
        return myEntries;
    }


}

片段

public class ListItFragment extends Fragment {

    private FragmentListItBinding mBinding;
    private ViewModelListItFragment mViewModel;
    private String mCategory;

    public ListItFragment(String category) {
        super();
        this.mCategory = category;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Timber.i("List it fragment created");
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_list_it, container, false);

        ViewModelFactoryListItFragment factoryListItFragment = new ViewModelFactoryListItFragment(this.getActivity().getApplication(), mCategory);
        mViewModel = new ViewModelProvider(this, factoryListItFragment).get(ViewModelListItFragment.class);
        LiveData<List<ListItEntry>> entries = mViewModel.getMyEntries();
        entries.observe(this.getViewLifecycleOwner(), listItEntries -> {
            if(listItEntries == null || listItEntries.size() == 0){
                mBinding.textEmpty.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.INVISIBLE);
            }

            mBinding.textEmpty.setVisibility(View.INVISIBLE);
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        });
        return mBinding.getRoot();

    }

我还没有实现 recylerview 适配器,所以它不会显示现有数据,但是当条目不存在时它也不会显示空文本视图。

    entries.observe(this.getViewLifecycleOwner(), listItEntries -> {
            if(listItEntries == null || listItEntries.size() == 0){
                mBinding.textEmpty.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.INVISIBLE);
            } else {
                mBinding.textEmpty.setVisibility(View.INVISIBLE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
            }
        });

你的代码在 if 部分之外,因此即使满足你的条件,你的 if 之外的部分正在执行。添加 else 部分以确保它不会如上所示执行