mutablelivedata

How to databind livedata object (android)

感情迁移 提交于 2021-02-08 09:56:50
问题 Today I've some question about mvvm and databinding on android, I'm trying to bind object properties on view. I've an Object (Model) with some properties, by example : public String name; public String title; public int value; I've a ViewModel with livedata like this : MutableLiveData<Object> _obj = new MutableLiveData<>(); public LiveData<Object> obj = _obj; And, at last, I've a view like this : <layout> <data> <variable name="viewModel"> type="com.sample.app.viewmodels.MainViewModel" /> <

How to use Single Live Event to show toast in Kotlin

巧了我就是萌 提交于 2021-01-29 15:22:43
问题 I want to use single live event class to show toast (like flag) Here is my code what I tried. I want to not use peding like flag. how do I fix it? MainViewModel class MainViewModel(private val movieRepository: MovieRepository) : ViewModel() { val keyword = MutableLiveData<String>() val movieList = MutableLiveData<List<Movie>>() val msg = MutableLiveData<String>() val pending: AtomicBoolean = AtomicBoolean(false) fun findMovie() { val keywordValue = keyword.value ?: return pending.set(true) if

How to observe the livedata with multiple observe for correct way in Android?

杀马特。学长 韩版系。学妹 提交于 2020-07-03 07:27:46
问题 I have an Activity, and it will turn to the fragment-A and then turn to the fragment-B like the following. Activity -> Fragment-A -> Fragment-B situation 1 These two fragment observe the same LiveData for showing snackBar like the following . viewModel.responseData.observe(this, Observer { it.getContentIfNotHandled()?.let { showSnackBar(it) } The livedata in viewModel: var responseData = MutableLiveData<Event<String>>() responseData.value = Event("$message") Error: When I use the above code.

why my map livedata is not called even though I have change the value?

此生再无相见时 提交于 2020-04-18 05:27:44
问题 I am new in MVVM and Android Architecture component. so I have repository like this object RestaurantRepository { val restaurants : LiveData<ArrayList<Restaurant>> = RestaurantClient.restaurants private var count : LiveData<Int> = Transformations.map(restaurants) { Log.d("debugLog","map") it.size } fun searchRestaurants(query: String, latitude:Double, longitude: Double) { mQuery = query RestaurantClient.searchRestaurants( query = query, latitude = latitude, longitude = longitude, start = 0 )

Difference of setValue() & postValue() in MutableLiveData

寵の児 提交于 2020-02-11 01:41:26
问题 There are two ways that make change value of MutableLiveData . But what is difference between setValue() & postValue() in MutableLiveData . I could not find documentation for same. Here is class MutableLiveData of Android. package android.arch.lifecycle; /** * {@link LiveData} which publicly exposes {@link #setValue(T)} and {@link #postValue(T)} method. * * @param <T> The type of data hold by this instance */ @SuppressWarnings("WeakerAccess") public class MutableLiveData<T> extends LiveData<T

Difference of setValue() & postValue() in MutableLiveData

人走茶凉 提交于 2020-02-11 01:38:48
问题 There are two ways that make change value of MutableLiveData . But what is difference between setValue() & postValue() in MutableLiveData . I could not find documentation for same. Here is class MutableLiveData of Android. package android.arch.lifecycle; /** * {@link LiveData} which publicly exposes {@link #setValue(T)} and {@link #postValue(T)} method. * * @param <T> The type of data hold by this instance */ @SuppressWarnings("WeakerAccess") public class MutableLiveData<T> extends LiveData<T

How to observe changes in database in order to update LiveData

半世苍凉 提交于 2020-01-13 13:36:09
问题 I am migrating an app from a LoaderManager with Callbacks to an implementation using ViewModel and LiveData . I would like to keep using the existing SQLiteDatabase . The main implementation works OK. The Activity instantiates the ViewModel and creates an Observer which updates the View if it observes changes in the MutableLiveData that lives in the ViewModel . The ViewModel gets it data (cursor) from the SQLiteDatabase through a query using a ContentProvider . But I have other activities

Not able to import InstantTaskExecutorRule in my 'jUnit' test case even after adding 'core-testing' dependency - Android testing

纵然是瞬间 提交于 2019-12-24 03:42:53
问题 I am writing test cases for my LoginViewModel . Where I want to perform setValue() operation on my MutableLiveData . To avoid Method getMainLooper in android.os.Looper not mocked Exception I am trying to add the following Rule in my ViewModel file inside test folder . @Rule public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule(); for that, I have added the following dependency: androidTestImplementation 'android.arch.core:core-testing:1.1.1' as dependency. But

Room not returning duplicates

北战南征 提交于 2019-12-11 19:35:35
问题 So I have a room database all set up, everything is fine, so I can make queries and inserts, delete etc no problems, however i've just run into a situation where id like to return entries by their Ids and duplicates should be allowed, however room is removing the duplicates, so for instance I send it a list of ids say <1,2,3,2,3> and it returns items by their ids but only sends me <1,2,3> removing the duplicate entries. The query I'm making is below (btw complete noob at sql) @Query("SELECT *

Fragment not receiving LiveData updates after remove + add

China☆狼群 提交于 2019-12-11 11:55:12
问题 I am currently playing around to get a hang of the fragment 's lifecycle in relation to ViewModel and LiveData . I have 2 fragments , fragmentA and fragmentB . I add the Observer in the onCreate method of each fragment . @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); sharedViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); sharedViewModel.getText().observe(this, new Observer<CharSequence>() { @Override public