android-architecture-components

Android ViewModel LiveData update view on button click

徘徊边缘 提交于 2020-01-13 19:21:08
问题 I am following this tutorial to learn ViewModel and LiveData. In my case, instead of getting data from network, I am simply generating random string on button click and trying to update a textview. The problem is that the textview does not get updated when the data is changed by button click, but only gets updated when orientation is toggled. Activity Class (extends LifecycleActivity) public class PScreen extends BaseActivity { @Override protected void onCreate(@Nullable Bundle

Refresh data from a network using LiveData

纵饮孤独 提交于 2020-01-13 10:13:53
问题 I am working on an app that queries the github api to get a list of user and i'm following the recommended android architecture component guide. Once the data is fetched from the network, I store it locally using Room DB and then display it on the UI using ViewModel that observes on the LiveData object (this works fine). However, I want to be able to have a button which when clicked would trigger a refresh action and perform a network request to get new data from the API if and only if there

Refresh data from a network using LiveData

試著忘記壹切 提交于 2020-01-13 10:12:07
问题 I am working on an app that queries the github api to get a list of user and i'm following the recommended android architecture component guide. Once the data is fetched from the network, I store it locally using Room DB and then display it on the UI using ViewModel that observes on the LiveData object (this works fine). However, I want to be able to have a button which when clicked would trigger a refresh action and perform a network request to get new data from the API if and only if there

Paging library DataSource.Factory for multiple data sources

放肆的年华 提交于 2020-01-13 02:45:14
问题 The new paging library allows us to specify a custom data source to use with data pagination. Paging library documentation and sample code on github show us how to create your custom data source instances by creating a subclass of DataSource.Factory like so: class ConcertTimeDataSourceFactory(private val concertStartTime: Date) : DataSource.Factory<Date, Concert>() { val sourceLiveData = MutableLiveData<ConcertTimeDataSource>() override fun create(): DataSource<Date, Concert> { val source =

Convert LiveData to MutableLiveData

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-12 15:47:10
问题 Apparently, Room is not able to handle MutableLiveData and we have to stick to LiveData as it returns the following error: error: Not sure how to convert a Cursor to this method's return type I created a "custom" MutableLiveData in my DB helper this way: class ProfileRepository @Inject internal constructor(private val profileDao: ProfileDao): ProfileRepo{ override fun insertProfile(profile: Profile){ profileDao.insertProfile(profile) } val mutableLiveData by lazy { MutableProfileLiveData() }

Nullability and LiveData with Kotlin

六眼飞鱼酱① 提交于 2020-01-12 14:11:24
问题 I want to use LiveData with Kotlin and have values that should not be null. How do you deal with this? Perhaps a wrapper around LiveData? Searching for good patterns here .. As an example: class NetworkDefinitionProvider : MutableLiveData<NetworkDefinition>() { val allDefinitions = mutableListOf(RinkebyNetworkDefinition(), MainnetNetworkDefinition(), RopstenNetworkDefinition()) init { value = allDefinitions.first() } fun setCurrent(value: NetworkDefinition) { setValue(value) } } I know value

Nullability and LiveData with Kotlin

无人久伴 提交于 2020-01-12 14:08:55
问题 I want to use LiveData with Kotlin and have values that should not be null. How do you deal with this? Perhaps a wrapper around LiveData? Searching for good patterns here .. As an example: class NetworkDefinitionProvider : MutableLiveData<NetworkDefinition>() { val allDefinitions = mutableListOf(RinkebyNetworkDefinition(), MainnetNetworkDefinition(), RopstenNetworkDefinition()) init { value = allDefinitions.first() } fun setCurrent(value: NetworkDefinition) { setValue(value) } } I know value

Display back button on toolbar on first screen of navigation graph

牧云@^-^@ 提交于 2020-01-12 09:50:47
问题 I'm trying out androidx navigation component and have setup my activity with a toolbar a container. I'm doing this in one of the intermittent screens of my app which has a lot of internal navigation/steps and I thought I could navigation arch component a try with this. Since this is an intermittent screen, I want to display a back button on the toolbar from the first screen itself. I've already setup the toolbar with the below code in the onCreate() method of my host activity,

Navigation destination unknown to this NavController after an activity result

本小妞迷上赌 提交于 2020-01-12 03:36:07
问题 i'm using nav controller 1.0.0alpha05 and it is working great, but i'm struggling with this dreaded error when i execute a navigation action after an activity result. I have a single activity/multiple fragments structure, in particular a fragment with a list of items and another one with the form for adding a new one. When i add another one without any picture it is working and returning to the previous one with the list of items, but when i take some photos i get the exception during the

LiveData.getValue() returns null with Room

為{幸葍}努か 提交于 2020-01-09 07:14:19
问题 Java POJO Object public class Section { @ColumnInfo(name="section_id") public int mSectionId; @ColumnInfo(name="section_name") public String mSectionName; public int getSectionId() { return mSectionId; } public void setSectionId(int mSectionId) { this.mSectionId = mSectionId; } public String getSectionName() { return mSectionName; } public void setSectionName(String mSectionName) { this.mSectionName = mSectionName; } } My Query method @Query("SELECT * FROM section") LiveData<List<Section>>