android-livedata

Why LiveData observer is being triggered twice for a newly attached observer

半城伤御伤魂 提交于 2019-12-02 17:01:50
My understanding on LiveData is that, it will trigger observer on the current state change of data, and not a series of history state change of data. Currently, I have a MainFragment , which perform Room write operation, to change non-trashed data , to trashed data . I also another TrashFragment , which observes to trashed data . Consider the following scenario. There are currently 0 trashed data . MainFragment is the current active fragment. TrashFragment is not created yet. MainFragment added 1 trashed data . Now, there are 1 trashed data We use navigation drawer, to replace MainFragment

LiveData not Observing after first Call

心已入冬 提交于 2019-12-02 10:14:40
I implemented LiveData & ViewModel to mimic AsyncTaskLoader. I load file names from the camera directory in DCIM, and then i attach a fileObserver to Observe when a File (picture) is deleted, and then a callback tells the LiveData to re-fetch the fileNames when delete event occurs The Problem: The code below should fetch the file Names from DCIM/Pictures asynchronously with the help of LiveData and then a FileObserver is attached to the directory (DCIM/Pictures), to monitor when a file is deleted and a callback is implemented with the LiveData sub-class to reload the files, as demonstrated in

Resume flowable converted to live data after screen rotation

China☆狼群 提交于 2019-12-02 08:08:33
问题 Say I have an activity like this: public class TestActivity extends AppCompatActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TextView countdown = new TextView(this); setContentView(countdown); ViewModelProviders.of(this) .get(TestViewModel.class) .getCountdown() .observe(this, countdown::setText); } } And the view model is: class TestViewModel extends ViewModel { private final LiveData<String> countdown =

Using CalendarView with databinding

巧了我就是萌 提交于 2019-12-01 11:23:04
I want to use two-way databinding with Android LiveData components ( as an alternative for Observable fields . Here's code for simple project with CalendarView and EditText that displays both info on button clicked. <?xml version="1.0" encoding="utf-8"?> <layout> <data> <variable name="testDate" type="android.arch.lifecycle.MutableLiveData<Long>" /> <variable name="testString" type="android.arch.lifecycle.MutableLiveData<String>" /> </data> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas

Using CalendarView with databinding

☆樱花仙子☆ 提交于 2019-12-01 08:13:25
问题 I want to use two-way databinding with Android LiveData components (as an alternative for Observable fields. Here's code for simple project with CalendarView and EditText that displays both info on button clicked. <?xml version="1.0" encoding="utf-8"?> <layout> <data> <variable name="testDate" type="android.arch.lifecycle.MutableLiveData<Long>" /> <variable name="testString" type="android.arch.lifecycle.MutableLiveData<String>" /> </data> <LinearLayout xmlns:android="http://schemas.android

Cannot find the setter for attribute in Data binding

守給你的承諾、 提交于 2019-12-01 03:46:18
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.swapData(items) } } } } and my reyclerView in XML assigned to this bindingAdapter like this: <android

ViewPager with viewmodel and live data , all 6 tabs data is replaced by last tab data

醉酒当歌 提交于 2019-12-01 03:38:22
问题 I am working on a ViewPager with 6 tabs where it has only one fragment TimesListFragment Depending on the arguments passed to TimesListFragment it calls api eg; science , technology, travel etc I have followed Google's GithubBrowserSample for my app I have TimesListFragment -> TimesViewModel -> TimesRepository There are 6 tabs , when I hit the api all the tabs show the same result which if of the last argument StoriesPagerAdapter.kt class StoriesPagerAdapter(fragmentManager: FragmentManager?)

Correct way to expose MutableLiveData as LiveData?

ⅰ亾dé卋堺 提交于 2019-11-30 17:11:28
问题 Consider the following ways to expose MutableLiveData : Method A class ThisViewModel : ViewModel() { private val _someData = MutableLiveData(true) val someData: LiveData<Boolean> get() = _someData } // Decompiled Kotlin bytecode public final class ThisViewModelDecompiled extends ViewModel { private final MutableLiveData _someData = new MutableLiveData(true); @NotNull public final LiveData getSomeData() { return (LiveData)this._someData; } } Method B class ThatViewModel : ViewModel() { private

LiveData Transformations.map() with multiple arguments

邮差的信 提交于 2019-11-30 15:32:35
问题 I have a value in the UI that it's value depends on two LiveData objects. Imagine a shop where you need a subtotal = sum of all items price and a total = subtotal + shipment price . Using Transformations we can do the following for the subtotal LiveData object (as it only depends on itemsLiveData ): val itemsLiveData: LiveData<List<Items>> = ... val subtotalLiveData = Transformations.map(itemsLiveData) { items -> getSubtotalPrice(items) } In the case of the total it would be great to be able

How can I do databinding with livedata?

半腔热情 提交于 2019-11-30 09:02:32
问题 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`