Home > Software engineering >  Data not displaying through ViewModel
Data not displaying through ViewModel

Time:04-25

I am newbie to android development and not able to understand why the data is being not displayed. initially the lists are empty so it should show empty text view but it is not showing that also.

Here's the code
Viewmodel factory

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


}

Fragment

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();

    }

I have not yet implemented recylerview adapter so it will not show existing data but when the entries are not there then also it is not showing empty textView.

CodePudding user response:

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

Your code is outside if part thus even though your condition is met the part outside your if is being executed. Add an else part to make sure that it doesn't get executed as shown above

  • Related