android-viewmodel

Observer not triggered on room database change

白昼怎懂夜的黑 提交于 2021-02-09 07:56:20
问题 I am trying to update a RecyclerView when a room database is changed. However the onChanged method of the observer defined in the MainActivity does not get called, when a change to the database occurs. If I let the DAO return LiveData instead of a List<> and use LiveData in my ViewModel it works as intended. However I need MutableLiveData as I want to change my query to the database during runtime. My setup: MainActivity.java BorrowedListViewModel viewModel = ViewModelProviders.of(this).get

Observer not triggered on room database change

泄露秘密 提交于 2021-02-09 07:55:43
问题 I am trying to update a RecyclerView when a room database is changed. However the onChanged method of the observer defined in the MainActivity does not get called, when a change to the database occurs. If I let the DAO return LiveData instead of a List<> and use LiveData in my ViewModel it works as intended. However I need MutableLiveData as I want to change my query to the database during runtime. My setup: MainActivity.java BorrowedListViewModel viewModel = ViewModelProviders.of(this).get

Observer not triggered on room database change

∥☆過路亽.° 提交于 2021-02-09 07:53:45
问题 I am trying to update a RecyclerView when a room database is changed. However the onChanged method of the observer defined in the MainActivity does not get called, when a change to the database occurs. If I let the DAO return LiveData instead of a List<> and use LiveData in my ViewModel it works as intended. However I need MutableLiveData as I want to change my query to the database during runtime. My setup: MainActivity.java BorrowedListViewModel viewModel = ViewModelProviders.of(this).get

How to have generic ViewModel in BaseActivty class

怎甘沉沦 提交于 2021-02-07 20:16:45
问题 I want to have a base activity class that takes care of some initialization I started defining it like this. abstract class BaseActivity<VIEW_MODEL : ViewModel, BINDING : ViewDataBinding> : AppCompatActivity() { lateinit var viewmodel: VIEW_MODEL lateinit var binding: BINDING lateinit var glide: RequestManager @get:LayoutRes abstract val layoutResource: Int override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this,

How to have generic ViewModel in BaseActivty class

守給你的承諾、 提交于 2021-02-07 20:14:27
问题 I want to have a base activity class that takes care of some initialization I started defining it like this. abstract class BaseActivity<VIEW_MODEL : ViewModel, BINDING : ViewDataBinding> : AppCompatActivity() { lateinit var viewmodel: VIEW_MODEL lateinit var binding: BINDING lateinit var glide: RequestManager @get:LayoutRes abstract val layoutResource: Int override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this,

MVVM - Accessing ViewModel/SQLite in a BroadcastReceiver started from a notification when app is closed

。_饼干妹妹 提交于 2021-02-07 14:15:36
问题 I have a reminder notification that is sent every few days. The sending of that notification is triggered with a repeating AlarmManager . The notification itself is built in the onReceive of my BroadcastReceiver (as described here). So when onReceive is triggered, the app is not even open/running. Now at that point I want to access my (local) SQLite database and get the correct content to build the notification with, but how would I get a ViewModelProvider (xxx in the code) in this place to

MVVM - Accessing ViewModel/SQLite in a BroadcastReceiver started from a notification when app is closed

╄→尐↘猪︶ㄣ 提交于 2021-02-07 14:13:15
问题 I have a reminder notification that is sent every few days. The sending of that notification is triggered with a repeating AlarmManager . The notification itself is built in the onReceive of my BroadcastReceiver (as described here). So when onReceive is triggered, the app is not even open/running. Now at that point I want to access my (local) SQLite database and get the correct content to build the notification with, but how would I get a ViewModelProvider (xxx in the code) in this place to

MVVM - Accessing ViewModel/SQLite in a BroadcastReceiver started from a notification when app is closed

↘锁芯ラ 提交于 2021-02-07 14:12:40
问题 I have a reminder notification that is sent every few days. The sending of that notification is triggered with a repeating AlarmManager . The notification itself is built in the onReceive of my BroadcastReceiver (as described here). So when onReceive is triggered, the app is not even open/running. Now at that point I want to access my (local) SQLite database and get the correct content to build the notification with, but how would I get a ViewModelProvider (xxx in the code) in this place to

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;

Android UI testing: Why LiveData's observers are not being called?

柔情痞子 提交于 2021-01-29 16:17:35
问题 I have been trying, without success, to do some UI tests on Android. My app follows the MVVM architecture and uses Koin for DI. I followed this tutorial to properly set up a UI test for a Fragment with Koin, MockK and Kakao. I created the custom rule for injecting mocks, setup the ViewModel, and on the @Before call, run the expected answers and returns with MockK. The problem is that, even when the fragment's viewmodel's LiveData object is the same as the testing class's LiveData object, the