android-livedata

Android LiveData null error when trying to update object

我与影子孤独终老i 提交于 2020-01-06 06:43:40
问题 I am looking for help to better understand LiveData in Android. I have created an application that allows a user to view, edit or create entries in a db. I have used a viewmodel on the form activity to share information between the activity and fragments and to also make the data Lifecycle aware. The code works great when I load a entry from the database. Any changes are captured and saved back to the database However when I want to create a new entry using the same code I get Caused by: java

Trigger update for a LiveData member when another LiveData is updated in the view model

南楼画角 提交于 2020-01-06 02:29:30
问题 In a word game app I share a model between an activity and a fragment: public class MainViewModel extends AndroidViewModel { private LiveData<List<Game>> mGames; private final MutableLiveData<Game> mDisplayedGame = new MutableLiveData<>(); (please excuse the non-english text in the screenshot) The activity observes mGames being currently played by the user and updates the navigational drawer menu (see the left side of the above screenshot). The fragment observes mDisplayedGame and displays it

Filling an adapter from different LiveData sources

断了今生、忘了曾经 提交于 2020-01-04 06:25:30
问题 I'm playing with LiveData and want to understand what it can do. I want to fill my RecyclerView with data from different sources by switch(using filters if you like). Filtering values inside an adapter is not an option. So, I decided to use MediatorLiveData inside my view model. Dao: @Query("SELECT * FROM tasks WHERE completed = 0") LiveData<List<Task>> getActiveTasksLiveData(); @Query("SELECT * FROM tasks") LiveData<List<Task>> getAllTasksLiveData(); @Query("SELECT * FROM tasks WHERE

Activity cannot be converted to LifecycleOwner

痴心易碎 提交于 2020-01-03 16:52:52
问题 I would like to use Room with LiveData, and in other projects I already used it, but in this one, I can not get it to work. It can't convert my activity into Lifecycle activity when I try to observe the livedata, however, I'm using the AppCompatActivity, and I even tried to Override the getLifecycle method (which worked for me in previous projects). I even tried with AndroidX but still the same issue :( Here my activity (Part of it): import androidx.appcompat.app.AppCompatActivity; import

Why this Android Room insert does not work?

只愿长相守 提交于 2020-01-02 03:12:15
问题 I implemented caching with room but for some reason it got spoiled, either it does not insert or does not get the data, I've done lot of debugging though, but still no any clue... Can someone help with this? So, the picture is following: MainActivity: mArticleViewModel = ViewModelProviders.of(this).get(ArticleViewModel.class); mArticleViewModel.getArticleList(mPageNumber).observe(this, articles -> { /* doesn't matter */ }); ViewModel: public class ArticleViewModel extends AndroidViewModel {

Android LiveData - how to reuse the same ViewModel on different activities?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 19:30:28
问题 Example ViewModel: public class NameViewModel extends ViewModel { // Create a LiveData with a String private MutableLiveData<String> mCurrentName; public MutableLiveData<String> getCurrentName() { if (mCurrentName == null) { mCurrentName = new MutableLiveData<>(); } return mCurrentName; } } Main activity: mModel = ViewModelProviders.of(this).get(NameViewModel.class); // Create the observer which updates the UI. final Observer<String> nameObserver = textView::setText; // Observe the LiveData,

Android LiveData - how to reuse the same ViewModel on different activities?

大兔子大兔子 提交于 2019-12-31 19:29:02
问题 Example ViewModel: public class NameViewModel extends ViewModel { // Create a LiveData with a String private MutableLiveData<String> mCurrentName; public MutableLiveData<String> getCurrentName() { if (mCurrentName == null) { mCurrentName = new MutableLiveData<>(); } return mCurrentName; } } Main activity: mModel = ViewModelProviders.of(this).get(NameViewModel.class); // Create the observer which updates the UI. final Observer<String> nameObserver = textView::setText; // Observe the LiveData,

What is the difference between map() and switchMap() methods?

…衆ロ難τιáo~ 提交于 2019-12-31 17:38:13
问题 What is the difference between those 2 methods of the LiveData class? The official doc and tutorial are pretty vague on that. In the map() method the first parameter called source but in the switchMap() it called trigger . What's the rationale behind that? 回答1: As per the documentation Transformations.map() Applies a function on the value stored in the LiveData object, and propagates the result downstream. Transformations.switchMap() Similar to map, applies a function to the value stored in

Notify Observer when item is added to List of LiveData

老子叫甜甜 提交于 2019-12-31 09:09:10
问题 I need to get Observer event when item is added to List of LiveData. But as far as I understand event receives only when I replace the old list with new one. For example when I do the next: list.value = mutableListOf(IssuePost(UserEntity(name, email, photoUrl), issueEntity)) Observer gets event. But when I just add item to value, Observer is silent. Could you please give an advice how I can implement what I need? 回答1: Internally, LiveData keeps track of each change as a version number (simple

is observeForever lifecycle aware?

不羁岁月 提交于 2019-12-30 10:01:30
问题 I'm working with MVVM, and I have made different implementations of it, but one thing that is still making me doubt is how do I get data from a Repository (Firebase) from my ViewModel without attaching any lifecycle to the ViewModel. I have implemented observeForever() from the ViewModel, but I don't think that is a good idea because I think I should communicate from my repository to my ViewModel either with a callback or a Transformation. I leave here an example where I fetch a device from