android-livedata

LiveData remove Observer after first callback

你。 提交于 2019-11-30 07:45:23
问题 How do I remove the observer after I receive the first result? Below are two code ways I've tried, but they both keep receiving updates even though I have removed the observer. Observer observer = new Observer<DownloadItem>() { @Override public void onChanged(@Nullable DownloadItem downloadItem) { if(downloadItem!= null) { DownloadManager.this.downloadManagerListener.onDownloadManagerFailed(null, "this item already exists"); return; } startDownload(); model.getDownloadByContentId(contentId)

Android Arch Components ViewModel and LiveData trigger after screen rotation

早过忘川 提交于 2019-11-30 04:05:12
问题 I have a problem when using ViewModel and LiveData I am new using ViewModel and LiveData arch components and have the problem when using fragments and rotate the screen the observer get triggered... I tried to move the viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) in all the fragment lifecycle methods but with no success. My scenario is kind of really easy one: Login screen with email and password User clicks on the "login" button the viewmodel calls the login(email,

Cannot find the setter for attribute in Data binding

拟墨画扇 提交于 2019-11-30 03:49:19
问题 I am using LiveData, DataBinding, and Kotlin in my application. I defined a Binding Adapter for a RecyclerView like this: class MainListBindings { private val TAG = "TasksListBindings" companion object { @JvmStatic @SuppressWarnings("unchecked") @BindingAdapter("main_items") fun setItems(recyclerView: RecyclerView, items: MutableLiveData<List<Homeitem>>? = null) { val adapter: RecyclerMainAdapter = recyclerView.adapter as RecyclerMainAdapter //prevent use of null list items?.let { adapter

How to create LiveData which emits a single event and notifies only last subscribed observer?

孤街浪徒 提交于 2019-11-29 11:31:32
I created live data which emits a single event as in this example . My question is next: How to notify only last subscribed observer when the value in the LiveData changes? What comes to my mind is to store observers in the linked list in SingleLiveData class and then to call super.observe only if a passed observer is the same as the last element of the list. I'm not sure if this is the best approach. I want to use this mechanism to propagate FAB click events from activity to the fragments which are shown inside of the ViewPager. Fragments are dynamically added to view pager adapter, so let's

setValue and postValue on MutableLiveData in UnitTest

泄露秘密 提交于 2019-11-29 11:20:19
I try to test some methods on my Application but I get an error when calling mutablelivedata.postValue. Here is a snippet and the error message: @Test public void callStartScreenRepository(){ Observer<User> userObserver = mock(Observer.class); startScreenViewModel.returnUser().observeForever(userObserver); maennlich = new User(); maennlich.setVorname("Christian"); MutableLiveData<User> userTest = new MutableLiveData<>(); userTest.postValue(maennlich); when(startScreenRepository.userWebService("testUser")).thenReturn(userTest); verify(userObserver).onChanged(maennlich); } java.lang

How can I do databinding with livedata?

爱⌒轻易说出口 提交于 2019-11-29 10:44:26
How can I do databinding with livedata? activity_user_detail.xml: <data> <variable name="viewModel" type="com.test.viewmodel.UserViewModel" /> </data> <TextView android:id="@+id/tv_amount" android:layout_width="match_parent" android:text="@{viewModel.age}" .... UserViewModel.java: public class UserViewModel extends ViewModel { public LiveData<User> user; public void getUserById(UserDao userDao, String userId){ transaction = UserDao .load(userId); } } UserDao.java: @Query("SELECT * FROM `user` WHERE id = :userId") LiveData<User> load(String userId); UserDetailActivity.java: private

Android implement search with view model and live data

六月ゝ 毕业季﹏ 提交于 2019-11-29 07:50:27
I'm working on a project in android for a udacity course I'm currently trying to implement a search function while adhering to android architecture components and using firestore and room I'm fairly new to all these concepts so please point out anything that seems wrong. So I made a database repository to keep my firestore and room databases in sync and to deliver the data. I'm then using viewmodel and the observer pattern (I think) so my observer gets the data and looks for changes gives it to my adapter ( refreshMyList(List) ) which populates a recyclerview like this : contactViewModel =

Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change

这一生的挚爱 提交于 2019-11-29 05:44:39
I found that the LiveData returned by Dao will call its observer whenever the row is updated in DB, even if the LiveData value is obviously not changed. Consider a situation like the following example : Example entity @Entity public class User { public long id; public String name; // example for other variables public Date lastActiveDateTime; } Example Dao @Dao public interface UserDao { // I am only interested in the user name @Query("SELECT name From User") LiveData<List<String>> getAllNamesOfUser(); @Update(onConflict = OnConflictStrategy.REPLACE) void updateUser(User user); } Somewhere in

LiveData remove Observer after first callback

假如想象 提交于 2019-11-29 05:26:06
How do I remove the observer after I receive the first result? Below are two code ways I've tried, but they both keep receiving updates even though I have removed the observer. Observer observer = new Observer<DownloadItem>() { @Override public void onChanged(@Nullable DownloadItem downloadItem) { if(downloadItem!= null) { DownloadManager.this.downloadManagerListener.onDownloadManagerFailed(null, "this item already exists"); return; } startDownload(); model.getDownloadByContentId(contentId).removeObservers((AppCompatActivity)context); } }; model.getDownloadByContentId(contentId).observeForever

Best practice for using MediatorLiveData only by present data

て烟熏妆下的殇ゞ 提交于 2019-11-28 12:46:16
What is the best practice for using the MediatorLiveData with multiple sources? I have a MediatorLiveData in the ViewModel, that is accessed from the view for the data, that should finally be presented. The MediatorLiveData depends on multiple other LiveDatas. Some of them come from the repository layer, some of them have to be processed in the ViewModel before they can be accessed from the MediatorLiveData and some of them come from the View. So my current implementation looks like the following schema: public MyViewModel extends ViewModel { LiveData<Foo> liveData1; LiveData<Bar> liveData2;