简体   繁体   中英

how to add and delete <Object> from (and to) LiveData<List<Object>>?

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

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:

  1. Receive the ViewHolder's onSwipe event and call the remove method in your viewModel .
  2. This remove method should modify your currentList by removing the element. You can pass an id to remove method to make things easier.
  3. Because currentList is LiveData every observer will be notified.
  4. Then you need to fetch the random entry from Room Database .
  5. Add this random entry to currentList . You can use the Transformations API of LiveData for this.
  6. Once again, because currentList is LiveData , every Observer will be notified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM