Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert (or delete) single List item from LiveData

I was wondering, what are some common practice, to insert (or delete) single List item from LiveData?

Currently, here's what I plan to do.

I have the following LiveData

LiveData<List<Animal>> animalListLiveData;

This is how I plan to observe it.

animalListLiveData.observe(this, animalList -> {

    if (listView.getAdapter() == null) {
        // First time.

        ArrayAdapter<String> adapter = new ArrayAdapter<>(..., animalList);
        // Assign adapter to ListView
        listView.setAdapter(adapter);

    } else {
        // Update operation.

        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffUtilCallback(animalList, oldAnimalList));
        diffResult.dispatchUpdatesTo(listView.getAdapter());
    }

    oldAnimalList.clear(); 
    oldAnimalList.addAll(animalList);
});

This is how I'm going to insert a single item into LiveData

animalListLiveData.getValue().add(newAnimal);
// Inform observer.
animalListLiveData.setValue(animalListLiveData.getValue())

I feel that my method is overkill for such a simple update operation. If the List is huge, DiffUtil needs to scan through the entire List.

Other developers have the same feeling as I do. https://github.com/googlesamples/android-architecture-components/issues/135

No good solution suggested so far.

I was wondering, do you discover any good practice (pattern), to insert (or delete) single List item from LiveData?

like image 536
Cheok Yan Cheng Avatar asked Mar 31 '18 18:03

Cheok Yan Cheng


People also ask

How do I update my LiveData list?

Update LiveData objectsLiveData has no publicly available methods to update the stored data. The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object.

Is LiveData deprecated?

Don't worry, LiveData is not going to be deprecated.

In which method should you observe a LiveData object?

Google introduced Android architecture components which are basically a collection of libraries that facilitate robust design, testable, and maintainable apps. It includes convenient and less error-prone handling of LifeCycle and prevents memory leaks.

Is LiveData lifecycle aware?

The new concept about this LiveData is that it's lifecycle-aware, meaning it respects the lifecycle state of the app components (activities, fragments) and ensures that LiveData only updates the component (the observer) when it's in an active lifecycle state.


1 Answers

If you know exactly where the changes will be, for example in the case where new items come up chronologically, you can get away with manually reporting changes to the adapter.

If not, you somehow need to let the adapter know which items have changed.

At the end is the day, these are your options:

If inserting and deleting is deterministic, you can notify the adapter directly. No middleman required.

If not, you have two choices. Either notify the adapter that the entire dataset has changed or check each entry to validate positions (DiffUtil).

As a sidenote, you should be setting your adapter outside of the observer. Directly on onCreate (or similar), using an empty List that is predefined in the class. You can then edit this list in response to changes from the observable.

like image 184
Knossos Avatar answered Sep 29 '22 17:09

Knossos