Search code examples
javaandroidmvvmandroid-roomrx-java2

Cannot handover List<Obj> (got from Single<List<Obj>>) from ViewModel to Activity. RecyclerView stays empty. Where is the cause?


In Dao:

    @Query("SELECT * FROM person_table WHERE status = :statusname ORDER BY RANDOM() LIMIT 1")
Single<List<Person>> getGuyWho(String statusname);

In Repo:

public class PersonRepository {
    private Single<List<Person>> mGuyWho; 
    PersonRepository(Application application) {
        PersonRoomDatabase db = PersonRoomDatabase.getDatabase(application);        
        mPersonDao = db.PersonDao();
        mGuyWho = mPersonDao.getGuyWho("debil"); 
    }
    Single<List<Person>> getGuyWho() {
        return mGuyWho;
    }
}

In ViewModel:

public class PersonViewModel extends AndroidViewModel {
    private PersonRepository mRepository;
    CompositeDisposable composite = new CompositeDisposable();
    private Single<List<Person>> mGuyWho; 
    private List<Person> workList;
    public PersonViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PersonRepository(application);
        workList = new ArrayList<>(); //initializing workList
        mGuyWho = mRepository.getGuyWho();

        mGuyWho.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<List<Person>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    composite.add(d);
                }

                @Override
                public void onSuccess(List<Person> people) {
                    Log.d(TAG, "onSuccess: called");
                    workList.addAll(people);
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(TAG, "onError: called");
                    Toast.makeText(application, "NO DATA", Toast.LENGTH_SHORT).show();
                }
            });
    }
    public List<Person> getWorkList() {
        return workList;
    }
}

В Activity:

public class MudakActivity extends AppCompatActivity implements CardStackListener {
    List<Person> debilList;
    private PersonViewModel debilViewModel;
    private CardStackView debilCardStackView;
    private MudakAdapter debilAdapter;
    //declaring variables

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        debilAdapter = new DebilAdapter(new DebilAdapter.PersonDiff(), debilList); 
        debilCardStackView.setLayoutManager(debilCardStackLayoutManager);
        debilCardStackView.setAdapter(debilAdapter);
        //initializing other variables
        debilViewModel = new ViewModelProvider(this,
                ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()))
                .get(PersonViewModel.class);
        debilList = debilViewModel.getWorkList();
    }
}

The recyclerView (cardStackView) shows empty, though objects with "debil" statusname are there surely (when I call them using LiveData, they are shown in the view). I guess there is something I typed incorrectly in ViewModel, maybe not at the appropriate place. Can't find. Where the mistake can be and how to fix it?


Solution

  • workList is changing afterwards and you are using it before its get updated . better make workList a LiveData and observe the changes. When data is set to to list you have to notify the adapter for changes.

    public class PersonViewModel extends AndroidViewModel {
        private PersonRepository mRepository;
        CompositeDisposable composite = new CompositeDisposable();
        private Single<List<Person>> mGuyWho;
        public LiveData<List<Person>> workList = new MutableLiveData<>();
        public PersonViewModel(@NonNull Application application) {
            super(application);
            mRepository = new PersonRepository(application);
            workList = new ArrayList<>(); //initializing workList
            mGuyWho = mRepository.getGuyWho();
            mGuyWho.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SingleObserver<List<Person>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            composite.add(d);
                        }
                        @Override
                        public void onSuccess(List<Person> people) {
                            workList.setValue(people);
                        }
                        @Override
                        public void onError(Throwable e) { }
                    });
        }
    }
    

    Observe the LiveData in your component .

    debilViewModel.workList.observe(this, (Observer<List<Person>>) personList -> {
            debilAdapter.submitList(personList);
        });