android-architecture-components

How to delete/remove PagedListAdapter item

喜欢而已 提交于 2021-01-21 00:02:12
问题 Currently I am using Android Architecture Components for App development everything is working along with paging library Now I want to remove recyclerview Item using PagedListAdapter to populate this we required to add a data source and from data source list is updating using LiveData no I want to remove a item from list notifyItemRemoved() is working from PagedList I am getting this exception: java.lang.UnsupportedOperationException java.util.AbstractList.remove(AbstractList.java:638) 回答1:

View models for RecyclerView items

你离开我真会死。 提交于 2021-01-20 16:42:05
问题 My activity has a Google's ViewModel that fetches some model items. These items are then transformed into adapter items of a RecyclerView. There are also many types of adapter items supported by one RecyclerView. I would like to have separate view model object for each of these model objects so that I can have more complex logic encapsulated only within that "small" view model. Currently when I have some asynchronous logic (that needs to be stopped in onCleared()) that is related only to some

How can I perform LiveData transformations on a background thread?

允我心安 提交于 2021-01-20 15:29:10
问题 I have a need to transform one type of data, returned by a LiveData object, into another form on a background thread to prevent UI lag. In my specific case, I have: MyDBRow objects (POJOs consisting of primitive long s and String s); a Room DAO instance emitting these via a LiveData<List<MyDBRow>> ; and a UI expecting richer MyRichObject objects (POJOs with the primitives inflated into e.g. date/time objects) so I need to transform my LiveData<List<MyDBRow>> into a LiveData<List<MyRichObject>

How and where to use Transformations.switchMap?

梦想与她 提交于 2021-01-20 14:12:18
问题 In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the switchMap function. The official documentation of switchMap can be found here. Can somebody explain how and where to use the switchMap function with a practical example? 回答1: In the map() function LiveData userLiveData = ...; LiveData userName =

How and where to use Transformations.switchMap?

十年热恋 提交于 2021-01-20 14:11:11
问题 In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the switchMap function. The official documentation of switchMap can be found here. Can somebody explain how and where to use the switchMap function with a practical example? 回答1: In the map() function LiveData userLiveData = ...; LiveData userName =

Android Navigation - Is it possible to restore a bundle from upper fragment with popBackStack method?

拜拜、爱过 提交于 2021-01-04 14:47:16
问题 I was trying to pass data between fragments while I was passing from upper fragment to down fragment, but I don't want to use the navigate method. It adds a new destination to the process history, but I want back to the previous fragment, restore it data and refresh the view. Is it possible to do it with NavController and popBackStack method? 回答1: After few hours I found a strange solution. findNavController().popBackStack() cannot pass data to the previous fragment, but we can do it using

Android LiveData in androidTest returns null

做~自己de王妃 提交于 2021-01-04 03:23:28
问题 I'm running an androidTest instrumentation test and I have a method that returns LiveData from a DAO object using Room. I'm calling the method like so: val animal = roomDatabase.animalsDao().getAnimal(1) animal.observeForever(mMockObserver) assertNotNull(animal.value) I used Mockito to mock the observer: @Mock private lateinit var mMockObserver = Observer<Animal> This should return an instance of LiveData containing the Animal at id 1, but it's null. It's my understanding that in order for

How to correctly mock ViewModel on androidTest

别等时光非礼了梦想. 提交于 2020-12-29 05:24:05
问题 I'm currently writing some UI unit tests for a fragment, and one of these @Test is to see if a list of objects is correctly displayed, this is not an integration test, therefore I wish to mock the ViewModel . The fragment's vars: class FavoritesFragment : Fragment() { private lateinit var adapter: FavoritesAdapter private lateinit var viewModel: FavoritesViewModel @Inject lateinit var viewModelFactory: FavoritesViewModelFactory (...) Here's the code: @MediumTest @RunWith(AndroidJUnit4::class)

How to correctly mock ViewModel on androidTest

空扰寡人 提交于 2020-12-29 05:23:19
问题 I'm currently writing some UI unit tests for a fragment, and one of these @Test is to see if a list of objects is correctly displayed, this is not an integration test, therefore I wish to mock the ViewModel . The fragment's vars: class FavoritesFragment : Fragment() { private lateinit var adapter: FavoritesAdapter private lateinit var viewModel: FavoritesViewModel @Inject lateinit var viewModelFactory: FavoritesViewModelFactory (...) Here's the code: @MediumTest @RunWith(AndroidJUnit4::class)

Multi-module Navigation with Architecture Components

不想你离开。 提交于 2020-12-27 08:02:28
问题 So I have this structure for my modules in my current app. I haven't found any official documentation on multi-module navigation yet but I found this article regarding this so here's how my gradle files are: Feature 1 - Detail ... implementation project(":base") implementation project(":feature-2-detail") ... Feature 2 - Detail ... implementation project(":base") implementation project(":feature-1-detail") ... Feature 3 - Detail ... implementation project(":base") implementation project("