[英]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). recyclerView (cardStackView) 显示为空,尽管具有“debil”状态名称的对象肯定存在(当我使用 LiveData 调用它们时,它们会显示在视图中)。 I guess there is something I typed incorrectly in ViewModel, maybe not at the appropriate place.
我想我在 ViewModel 中输入了错误的东西,可能不是在适当的地方。 Can't find.
找不到。 Where the mistake can be and how to fix it?
错误可能在哪里以及如何解决?
workList
is changing afterwards and you are using it before its get updated. workList
之后会发生变化,而您在更新之前正在使用它。 better make workList
a LiveData
and observe the changes.最好让
workList
成为LiveData
并观察变化。 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.观察组件中的
LiveData
。
debilViewModel.workList.observe(this, (Observer<List<Person>>) personList -> {
debilAdapter.submitList(personList);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.