android-livedata

Android Architecture Components LiveData

大城市里の小女人 提交于 2019-12-24 06:08:46
问题 I'm trying to implement a simple App using Architecture Components. I can get the info from RestApi services using Retrofit2. I can show the info in the respective Recyclerview and when I rotate the phone everything works as it should. Now I want to filter by a new kind of object (by string) Can someone guide me a little with the ViewModel, I don't know what is the best practice to do that... I'm using MVVM... This is my ViewModel: public class ListItemViewModel extends ViewModel { private

When submitting a new list to RecyclerView ListAdapter the diff check always returns true for areContentsTheSame()

ぃ、小莉子 提交于 2019-12-23 23:24:22
问题 I am using an MVVM architechture to build a simple ordering app. I am using a RecyclerView in my ProductsFragment to list all the products that can be ordered. I am also using LiveData in my ViewModel and observable in my Fragment to check for any changes to the List of products. In my list item I have 3 buttons: 1 button to add a product to the basket, another to increment the quantity that customer wants to add to the basket and a third to decrement the quantity the customer wants to add to

MutableLiveData with BindingAdapter not updating visibility of view

泄露秘密 提交于 2019-12-23 19:13:19
问题 My BindingAdapter only runs once even though I update my LiveData multiple times. public class ButtonViewBindingAdapter { @BindingAdapter("hideIfZero") public static void setHideIfZero(View view, MutableLiveData<Integer> currentPosition) { view.setVisibility(currentPosition.getValue() == 0 ? View.GONE : View.VISIBLE); } } <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="viewModel" type="com.package

Android Live data observer exception

。_饼干妹妹 提交于 2019-12-23 18:52:49
问题 I am trying to implement the new android architecture components and have used live data in the fragment and view model but when I add an observer to the live data the app crashes throwing this exception. Process: com.nrs.nsnik.architecturecomponents, PID: 3071 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nrs.nsnik.architecturecomponents/com.nrs.nsnik.architecturec omponents.view.MainActivity}: java.lang.ClassCastException: android.arch.lifecycle.LiveData

EditText LiveData Two-way binding

女生的网名这么多〃 提交于 2019-12-23 09:26:59
问题 Okay, so I have a ViewModel with a getter getTitle() that returns MutableLiveData<String> . <EditText style="@style/Widget.EditText.FullWidth" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/label_title" android:inputType="text" android:text="@={ viewModel.title }" /> This works fine at first: the EditText contains the value of the MutableLiveData when it first appears. However, if the value of this data is updated using MutableLiveData.setValue(

Why is the value param nullable in Observer from Android Architecture Components?

时光毁灭记忆、已成空白 提交于 2019-12-23 07:46:10
问题 LiveData from architecture components defines an Observer with nullable value for the receiver callback: public interface Observer<T> { /** * Called when the data is changed. * @param t The new data */ void onChanged(@Nullable T t); } Why is there an explicitly nullable annotation? The doc of LiveData.observe() also says: If LiveData already has data set, it will be delivered to the observer. E.g. Observer waits for non-nullable updates or immediately receive previous non-nullable value, that

android livedata make sequential call

感情迁移 提交于 2019-12-23 07:24:58
问题 I am using Retrofit, Live data. There is one situation on my project, I have to make sequence of network call. if any one fails it should return error. At present I have two live data observers to get the work done, which is not good approach so I wanted to know the better approach or sample code to handle such requirement. Note: I am not using Rxjava. View code Basic logic String id = "items/1233"; //ID which has to to be deleted if (isCustomizedItem) { viewModel.deleteEvent(id); } else {

android livedata make sequential call

这一生的挚爱 提交于 2019-12-23 07:21:06
问题 I am using Retrofit, Live data. There is one situation on my project, I have to make sequence of network call. if any one fails it should return error. At present I have two live data observers to get the work done, which is not good approach so I wanted to know the better approach or sample code to handle such requirement. Note: I am not using Rxjava. View code Basic logic String id = "items/1233"; //ID which has to to be deleted if (isCustomizedItem) { viewModel.deleteEvent(id); } else {

What is lifecycle observer and how to use it correctly?

心不动则不痛 提交于 2019-12-22 07:02:01
问题 I have read about new architectural components in Android. So, i wanted to ask what are lifecycle observers and why do we need them? In what cases it is useful? Thanks for your answer! 回答1: You can use ProcessLifecycleOwner for Applications LifeCycle Event. You can implement Lifecycler Observer in your Application Class public class MyApplication extends MultiDexApplication implements LifecycleObserver @Override public void onCreate() { super.onCreate(); ProcessLifecycleOwner.get()

Android MediatorLiveData observer

旧巷老猫 提交于 2019-12-21 09:22:06
问题 I'm a bit confused on why the following code doesn't work: MutableLiveData<String> mutableTest = new MutableLiveData<>(); MediatorLiveData<String> mediatorTest = new MediatorLiveData<>(); mediatorTest.addSource(mutableTest, test -> { Timber.d(test); }); mutableTest.setValue("bla!"); This code seems straightforward, however the debugger doesn't enter the callback and nothing is logged to the console... Edit: shouldn't this work then? MutableLiveData<String> mutableTest = new MutableLiveData<>(