Reputation: 257
I have a recyclerView which is filled with LiveData<List> currentList. I take Single<List> and add person using currentList.setValue(people) in onSuccess method (see code below). But when I open the app, there is just 1 object in the recyclerView. My task is: 1) when app is open it has to add 4 persons to recyclerView (i.e. to currentList); 2) after onSwipe method it has to delete 1st and add 1 new random from RoomDatabase to the end of currentList.
It means that I need methods which are deleting and adding objects to LiveData<List>. How can I do this? (ViewModel code is below)
public class PersonViewModel extends AndroidViewModel {
private PersonRepository mRepository;
private CompositeDisposable composite = new CompositeDisposable();
private Single<List<Person>> mGuyWho; // это нада??? LOBNYA
private Single<Person> mGuy; // PSKOV
private MutableLiveData<List<Person>> currentList = new MutableLiveData<>();
public PersonViewModel(@NonNull Application application) {
super(application);
mRepository = new PersonRepository(application);
mGuyWho = mRepository.getGuyWho(); //это нада?? LOBNYA
mGuy = mRepository.getGuy(); // PSKOV
mGuyWho.subscribeOn(Schedulers.io()) // LOBNYA nachalo
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<Person>>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe(Disposable d): called");
composite.add(d);
}
@Override
public void onSuccess(List<Person> people) {
Log.d(TAG, "onSuccess: called");
currentList.setValue(people); // here I pass List to LiveData<List>
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError: called");
Toast.makeText(application, "NO DATA", Toast.LENGTH_SHORT).show();
}
}); // LOBNYA konets
mGuy.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Person>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe(Disposable d): called");
composite.add(d);
}
@Override
public void onSuccess(Person person) {
Log.d(TAG, "onSuccess: called");
currentList.setValue(person); // there is an error, for I cannot bypass <Person> to LiveData<List<>>
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError: called");
}
})
}
LiveData<List<Person>> getCurrentList() {
return currentList;
}
};
Upvotes: 0
Views: 301
Reputation: 1234
Your LiveData
is composed of a List<Person>
object:
private MutableLiveData<List<Person>> currentList = new MutableLiveData<>();
In your onSuccess()
method, you are trying to pass it a Person
object. You should instead pass mutableListOf(Person())
in there for it to work. However, this still won't solve your problem.
What you need to do is the following:
onSwipe
event and call the remove
method in your viewModel
.remove
method should modify your currentList
by removing the element. You can pass an id
to remove
method to make things easier.currentList
is LiveData
every observer
will be notified.Room Database
.currentList
. You can use the Transformations
API of LiveData
for this.currentList
is LiveData
, every Observer
will be notified.Upvotes: 1