android-viewmodel

Room database: getting SELECT MAX() twice after creating new RecyclerView item

你说的曾经没有我的故事 提交于 2020-01-06 08:10:57
问题 I have a RecyclerView list of CardView items that is working properly. Upon creation of a new CardView that is inserted into the database, I would like to fire a Toast that informs the user that the CardView was successfully added and show the CardView number. The CardView number is the Id of the CardView item inserted into the database. The data is saved to the database when the user clicks on a Save button that fires onClickSave(). I set up an @Query in the Dao to get the MAX(cardId): Dao .

Android: update single Room column with Dao call from repository?

隐身守侯 提交于 2020-01-06 05:51:10
问题 I have a Room database and RecyclerView that shows a list of CardViews. One of the database columns holds Sort Index values so the CardViews can easily be sorted and moved within the RecyclerView list. I have a Dao call to update the Sort Index values whenever a CardView is moved, deleted or added. Room requires CRUD on a background thread and the syntax I have in the AsyncTask() in the respository that calls the Dao method is wrong. I am trying to pass the individual "int" sortOrders and the

'ViewModelProviders' is deprecated. After upgrading lifecycle-extensions:2.1.0 to 2.2.0-alpha05

十年热恋 提交于 2020-01-04 20:45:03
问题 Android Studio -> New Project -> Choice "Tabbed Activity". After upgrading lifecycle-extensions from 2.1.0 to 2.2.0-alpha05 ViewModelProviders is deprecated. In simple cases it replaceable by "by viewModels()" for example: plHlFrModel = ViewModelProviders.of(this).get(PlHlFrModel::class.java) replaced by: private val plHlFrModel by viewModels<PlHlFrModel>() But I can't figure out how to replace this one: pageViewModel = ViewModelProviders.of(this) .get(PageViewModel::class.java).apply {

Filling an adapter from different LiveData sources

断了今生、忘了曾经 提交于 2020-01-04 06:25:30
问题 I'm playing with LiveData and want to understand what it can do. I want to fill my RecyclerView with data from different sources by switch(using filters if you like). Filtering values inside an adapter is not an option. So, I decided to use MediatorLiveData inside my view model. Dao: @Query("SELECT * FROM tasks WHERE completed = 0") LiveData<List<Task>> getActiveTasksLiveData(); @Query("SELECT * FROM tasks") LiveData<List<Task>> getAllTasksLiveData(); @Query("SELECT * FROM tasks WHERE

How to inject dependencies in ViewModels using Dagger2

自古美人都是妖i 提交于 2020-01-04 05:57:35
问题 I am trying to implement new architecture (MVVM + RxJava2 + Dagger2 + Retrofit) in my existing project. I have set up whole above architecture and tested on HomeActivity. Dependencies injected in HomeViewModel. So Now I was trying to inject dependencies same as HomeViewModel in a FollowingViewModel of FollowingFragment which is a container Fragment of HomeActivity.But injected dependencies always return null(Not Initiziling). I am following this project riggaroo/android-arch-components-date

Cannot create an instance of ViewModel class (Unable to start activity ComponentInfo)

核能气质少年 提交于 2020-01-03 09:24:19
问题 I am using MVVM, Retrofit, LiveData in my project but I get this error before that I saw these links Cannot create an instance of custom ViewModel Cannot create an instance of class ViewModel Error java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.orangehat.movieinfo/ir.orangehat.movieinfo.application.home.HomeActivity}: java.lang.RuntimeException: Cannot create an instance of class ir.orangehat.movieinfo.application.home.HomeViewModel This is my ViewModel and I think the

Optimizing Parent Fragment for ViewModel and DataBinding in order to avoid boilerplate code

天大地大妈咪最大 提交于 2019-12-25 01:47:17
问题 I have a ParentFragment and a ChildFragment . They are working pretty fine. My problem is that in the future I might create many child fragments and this makes me write this boilerplate code for every single child fragment. Thus, I would like to optimize my ParentFragment so that I do not have to write boilerplate code for every single new child framment I create in the future. ParentFragment abstract class ParentFragment<T: ViewDataBinding>: Fragment() { @LayoutRes abstract fun

In Android, if I use binding, how to apply ClickableSpan?

℡╲_俬逩灬. 提交于 2019-12-25 01:35:49
问题 I know that if binding is not applied, then I can use Textview.setText(ClickableSpan) to apply spannig effect to make part of the text clickable. However, my app used binding and the textview's text is binded to a ViewModel inside xml like android:text="@{model.certainString}" and in the ViewModel I declared String certainString = "" , in this case how do I apply spanning inside the ViewModel then? 回答1: You could write your own BindingAdapter which accepts either the model or the string value

android paging library - which DataSource I should use

两盒软妹~` 提交于 2019-12-24 08:34:04
问题 What I Want: I want to implement Android Paging Library with network call only. What I Understood: So, DataSource has three classes that I can extend. PageKeyedDataSource: If your web service has pageNumber in url then you can use it. ItemKeyedDataSource: When you are fetching nested data then you can use it. For example getting replies on particular comment. PositionalDataSource: You can use when you know exact number of data is going to load. What I Tried : Code is below. MainActivity.java

Android Live data observer exception

。_饼干妹妹 提交于 2019-12-23 18:52:49
问题 I am trying to implement the new android architecture components and have used live data in the fragment and view model but when I add an observer to the live data the app crashes throwing this exception. Process: com.nrs.nsnik.architecturecomponents, PID: 3071 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nrs.nsnik.architecturecomponents/com.nrs.nsnik.architecturec omponents.view.MainActivity}: java.lang.ClassCastException: android.arch.lifecycle.LiveData