android-livedata

How to properly reload liveData manually in Android?

大城市里の小女人 提交于 2021-02-07 04:01:46
问题 My app is a basic news app which fetches data from JSON provided by Guardian API. I parsed the values from JSON using raw java code (not using retrofit). Then I get the LiveData in NewsFeedViewModel class which extends as AndroidViewModel. And then in the fragment, I submit list to adapter. These are the issues I'm facing: 1) at first, if the articles to show is set to 10, then if i go to settings and change it to 2, then the last 8 articles are disappearing but the white space /gap is not

LiveData not updating when data changes

断了今生、忘了曾经 提交于 2021-02-06 11:51:30
问题 I'm using LiveData to fetch data from a server and observe it. My onChanged() method just gets called the first time, and does not get called when data in the server gets updated. UserFragment: UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class); userViewModel.getUser().observe(this, new Observer<User>() { @Override public void onChanged(User user) { //Set UI } }); UserViewModel: public class UserViewModel extends AndroidViewModel { private LiveData<User> user;

How to update LiveData by set a value to variable

江枫思渺然 提交于 2021-01-29 14:07:18
问题 I have a variable and a LiveData, and I want to update the LiveData when I set a value to the variable. var trigger = "" val updateValue: LiveData<Value> = service.getValue(trigger) // when set the value1 to trigger, the updateValue need to update the value. var trigger = "value1" val updateValue: LiveData<Value> = service.getValue("value1") 回答1: Use switchmap : var trigger = MutableLiveData<String>() val updateValue: LiveData<Value> = Transformations.switchMap(trigger) { service.getValue(it)

How can I set a ConstraintLayout Group's visibility via DataBinding?

懵懂的女人 提交于 2021-01-29 10:49:43
问题 I have 2 Group s in my layout which control the visibility of my Views. However, I cannot set their visibility via DataBinding: <layout> <data> <import type="android.view.View"/> <variable name="viewModel" type="co.aresid.book13.fragments.trackinglist.TrackingListViewModel" /> </data> <androidx.constraintlayout.widget.ConstraintLayout> ... <androidx.constraintlayout.widget.Group android:id="@+id/content_group" android:layout_width="0dp" android:layout_height="0dp" android:visibility="@

Get the result of retrofit async call

ぃ、小莉子 提交于 2021-01-29 09:04:30
问题 I tried to use retrofit to get the response data of web api, but the result of response data seems not sync as the same. fun fetchData(): LiveData<String> { val auth = Credentials.basic(name, pass) val request: Call<JsonElement> = webApi.fetchData() val response: MutableLiveData<String> = MutableLiveData() request.enqueue(object : Callback<JsonElement> { override fun onFailure(call: Call<JsonElement>, t: Throwable) { Log.e(TAG, "Failed to fetch token", t) } override fun onResponse(call: Call

How to read DataBaseRoom and display a textView

陌路散爱 提交于 2021-01-29 06:49:21
问题 I am practicing with dataBaseRoom, for them I have created a button, which allow me to read database , i want to put the name of a user and when pressing the button (read data base) reflects me, his name, last name and age in the texview,the problem is that I do not know how to display the data in a textView. Activity where I want to carry out the actions //Entity @Entity(tableName = "user_table") data class User( @PrimaryKey(autoGenerate = true)@ColumnInfo(name = "id") val id: Int, val

Right place to offer or send Channel in MVI pattern

风流意气都作罢 提交于 2021-01-29 06:41:04
问题 I load data in recycleView in advance. In order to do that I have following code in onCreate() of Activity : override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setupUI() setupViewModel() observeViewModel() if (savedInstanceState == null) { mainViewModel.userIntent.offer(MainIntent.FetchUser) } } As you see I offer() when savedInstanceState is null, The problem is when we have process death ( you can simply create it

Livedata mathematical operations

半腔热情 提交于 2021-01-29 06:39:08
问题 I have two livedatas. I need to take subtraction on them, but how to do it with two livedatas? I've created something like this, but this is no proper way because it doesn't refresh result always when I need it. totalFragmentViewModel.getTotalExpenseValue().observe(getViewLifecycleOwner(), new Observer<Double>() { @Override public void onChanged(Double aDouble) { expenseTextView.setText(String.valueOf(aDouble)); mExpense += aDouble; balanceTextView.setText(String.valueOf(mIncome - mExpense));

ViewModel Unit testing multiple view states with LiveData, Coroutines and MockK

Deadly 提交于 2021-01-28 03:10:53
问题 I have a function in ViewModel with 2 states, first state is always LOADING, second state depends on result of api or db interactions. This is the function fun getPostWithSuspend() { myCoroutineScope.launch { // Set current state to LOADING _postStateWithSuspend.value = ViewState(LOADING) val result = postsUseCase.getPosts() // Check and assign result to UI val resultViewState = if (result.status == SUCCESS) { ViewState(SUCCESS, data = result.data?.get(0)?.title) } else { ViewState(ERROR,

Restoring Room database from a local backup file, LiveData ViewModel doesn't refresh

南笙酒味 提交于 2021-01-28 03:04:36
问题 I'm trying to restore a room database programmatically. I followed this tutorial to implement a view model using LiveData of Words list, so that the words list refresh automatically on screan when insert, delete or update. mWordViewModel.getAllWords().observe(this, new Observer<List<Word>>() { @Override public void onChanged(@Nullable final List<Word> Words) { // Update the cached copy of the Ussds in the adapter. adapter.setWords(Words); } }); I also used the following code to backup and