android-livedata

Reload RecyclerView after data change with Room, ViewModel and LiveData

非 Y 不嫁゛ 提交于 2019-12-21 05:11:10
问题 I am trying, without success, to solve a problem for days. I would like to update my recyclerView whenever the records of a particular model change in the Database (DB Room). I use ViewModel to handle the model data and the list of records are stored in LiveData. Database @Database(entities = arrayOf(Additive::class), version = ElementDatabase.DB_VERSION, exportSchema = false) abstract class ElementDatabase() : RoomDatabase() { companion object { const val DB_NAME : String = "element_db"

Reload RecyclerView after data change with Room, ViewModel and LiveData

主宰稳场 提交于 2019-12-21 05:11:06
问题 I am trying, without success, to solve a problem for days. I would like to update my recyclerView whenever the records of a particular model change in the Database (DB Room). I use ViewModel to handle the model data and the list of records are stored in LiveData. Database @Database(entities = arrayOf(Additive::class), version = ElementDatabase.DB_VERSION, exportSchema = false) abstract class ElementDatabase() : RoomDatabase() { companion object { const val DB_NAME : String = "element_db"

Accessing BroadCastReceiver in Viewmodel

不打扰是莪最后的温柔 提交于 2019-12-21 04:48:22
问题 I am struggling in choosing the right way to pass data from broadcastReceiver to ViewModel and from there I pass data to my Repository and update LiveData. I use FCM push notifications and have local broadCastReceiver which uses ActivityLifecycle. I found that it is bad practice to access ViewModel from BroadcastReceiver, but not sure why? If I manage lifecycle of broadcastReceiver it should not cause any problems... So what is the best way to pass received data from FCM to my Repository's

Live Data and 2-Way Data Binding: Custom setter not being called

风格不统一 提交于 2019-12-21 04:14:16
问题 I am using 2-way data binding to update a LiveData String object from my ViewModel with a string set in the EditText: <android.support.design.widget.TextInputEditText android:id="@+id/writeReviewTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={viewModel.liveReviewTitle}" /> So, from my understanding, the ViewModel would have its liveReviewTitle attribute updated every time the text changed in the EditText. I assume this is happening through the

How to make retrofit API call using ViewModel and LiveData

两盒软妹~` 提交于 2019-12-20 10:28:25
问题 this is the first time I'm trying to implement MVVM architecture, and I'm a bit confused about the correct way to make an API call. Currently, I'm just trying to make a simple query from the IGDB API, and output the name of the first item in a log. My activity is setup as follow: public class PopularGamesActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popular_games);

LiveData not Observing after first Call

北城以北 提交于 2019-12-20 06:18:12
问题 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

How to share an instance of LiveData in android app?

南楼画角 提交于 2019-12-17 21:16:34
问题 Simple use case I am using MVVM architecture and Android Architecture Components in my app. After user logs in, I am fetching multiple network data and want to have access to it from different ViewModels attached to different Activities lifecycle. I don't want to use Room Persistence Library in my app. I have seen some question about sharing a ViewModel between Activities or using a LiveData as static member, but I think most of the cases we need to access the same data in multiple screens. I

Observe LiveData from foreground service

落花浮王杯 提交于 2019-12-17 18:55:32
问题 I have a repository which holds the LiveData object and is used by both activity and a foreground service through a ViewModel. When I start observing from the activity everything works as expected. However observing from the service doesn't trigger the Observe. This is the code I use class MyService: LifecycleService() { lateinit var viewModel: PlayerServiceViewModel override fun onCreate() { viewModel = MyViewModel(applicationContext as Application) } override fun onStartCommand(intent:

LiveData update on object field change

扶醉桌前 提交于 2019-12-17 15:24:53
问题 I'm using Android MVVM architecture with LiveData. I have an object like this public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } And my view model looks like this public class InfoViewModel extends AndroidViewModel {

Login Example using Retrofit, MVVM, LiveData in android

有些话、适合烂在心里 提交于 2019-12-14 03:12:22
问题 I checked this article but observe the response changes in MainActivity. Here is my code for LoginRepo public MutableLiveData<LoginResponseModel> checkLogin(LoginRequestModel loginRequestModel) { final MutableLiveData<LoginResponseModel> data = new MutableLiveData<>(); Map<String, String> params = new HashMap<>(); params.put("email", loginRequestModel.getEmail()); params.put("password", loginRequestModel.getPassword()); apiService.checkLogin(params) .enqueue(new Callback<LoginResponseModel>()