android-viewmodel

Observing viewmodel for the second time returns null in android

≯℡__Kan透↙ 提交于 2019-12-13 13:06:57
问题 In my android app,im following architecture components with mvvm pattern. my app makes a network call to display the weather information.api call is being made from repository which returns a livedata of response to the viewmodel,which inturn is observed by my main activity. the app works fine except for one condition,whenever i disconnect the internet to test the fail case,it inflates error view as required in the error view i have a retry button,which makes the method call to observe the

Android ViewModel: Should I “borrow” the observe() method from LiveData like in the official example?

孤人 提交于 2019-12-13 10:29:26
问题 When working with ViewModels the View observes the ViewModel. It has to register as an observer. In the official tutorial of Google this registration is delegated to the observe() method of a LiveData object. public class MyViewModel extends ViewModel { private MutableLiveData<List<User>> users; public LiveData<List<User>> getUsers() { if (users == null) { users = new MutableLiveData<List<Users>>(); loadUsers(); } return users; } private void loadUsers() { // Do an asynchronous operation to

How to make this ViewModelFactory more flexible and accept different kinds of ViewModel classes?

心已入冬 提交于 2019-12-13 03:49:17
问题 I copied an example of MVVM with Android Architecture Components, Retrofit, Dagger, and data binding. I am using this code as a starting point to my app in order to start using better architectures in Android app development. However, take these codes: interface ViewModelInjector { /** * Injects required dependencies into the specified PostListViewModel. * @param postListViewModel PostListViewModel in which to inject the dependencies */ fun inject(postListViewModel: PostListViewModel)

Unable to open activity: cannot create instance of Android View Model

非 Y 不嫁゛ 提交于 2019-12-11 17:31:58
问题 I am creating an app to scan a barcode and get that information into a Room Database using Android View Model and LiveData. Here is the error message java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.davin.scoutingapp2019/com.example.davin.scoutingapp2019.DataDisplay}: java.lang.RuntimeException: Cannot create an instance of class com.example.davin.scoutingapp2019.TeamViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659) at

cannot find symbol method setUser_list_user_view(User_List_UserViewModel) though it is available in Binding Class

﹥>﹥吖頭↗ 提交于 2019-12-11 17:18:48
问题 While implementing MVVM architecture with Data Binding and Live Data run into problem with getting error "cannot find symbol method setUser_list_user_view(User_List_UserViewModel)" I have done many time rebuild, cleanup and other stuff but this error not going..I am doing this first time so not sure have implemented the right method. Below is my code. Thnx in advance for the help User_List_UserViewModel.java public class User_List_UserViewModel extends AndroidViewModel { private User_List

RecyclerView: how to refresh full List of CardViews with ViewModel?

守給你的承諾、 提交于 2019-12-11 14:58:04
问题 I have a Main Activity that extends AppCompatActivity with a RecyclerView. It is working fine to load a List of CardViews from a Room database and then filter the List. Problem occurs after pressing Back space to return the user to the original List. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(layout.activity_main); ... mViewModel = ViewModelProviders.of(MainActivity.this).get(MainViewModel.class); mViewModel.getAllCards()

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

Create ViewModel with Application

百般思念 提交于 2019-12-11 07:58:56
问题 I am trying to set data onto the ViewModel in a file in which I do not wish to hold any activity references. Class A -> set data onto the LiveData in ViewModel Has an Application class reference but does not hold activity or fragment Class B -> read data from the LiveData in ViewModel Has a reference to the activity. Class B can be destroyed and recreated along with the Activity's config changes. Class A is persistent in memory and keeps setting data to the LiveData Problem:

How can I make one ViewModel class return multiple data types to an activity or a fragment

拟墨画扇 提交于 2019-12-11 07:33:39
问题 I'm reading this blog post Using Android Architecture Components with Firebase Realtime Database (Part 2) and I'm implementing the last code snippet, and its working. private final FirebaseQueryLiveData liveData = new FirebaseQueryLiveData(HOT_STOCK_REF); private final MediatorLiveData<HotStock> hotStockLiveData = new MediatorLiveData<>(); public HotStockViewModel() { // Set up the MediatorLiveData to convert DataSnapshot objects into HotStock objects hotStockLiveData.addSource(liveData, new

How to get LiveData from Room?

我的梦境 提交于 2019-12-11 06:47:36
问题 I'm trying to get LiveData from Room. So my RecycleView can have Live updates if anything in database is changed I have tried with out LiveData and that works, but when i add LiveData that always shows this error. error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<com.example.models.Club>>). public abstract java.lang.Object getAll(@org.jetbrains.annotations.NotNull() I googled and looked on this site for solution, but every on with