android-livedata

Android Architecture component LiveData - how to bind broadcastReceivers to lifecycle

我只是一个虾纸丫 提交于 2019-12-07 09:23:26
问题 Using Android LiveData I'd like to be able to unregister and register many BroadcastReceiver s in the onInactive() and onActive() call backs. So I want to do something like this: public class BroadcastRecieverLiveData extends LiveData<BroadCastReciever> { private BroadcastReciever reciever; private Context context; public BroadcastRecieverLiveData(Context context) { this.context = context; } @Override protected void onActive() { IntentFilter filter = new IntentFilter(); filter.addAction("SOME

LiveData not updating data from one Activity to another Activity - Android

你说的曾经没有我的故事 提交于 2019-12-07 07:32:04
问题 I have 2 Activity viz List Activity Detail Activity The list Activity shows list of items and detail Activity is shown on clicking an item from the list. In the ListActivity we observe for fetching the feeds from the DB and once we do, we update the UI. List page feedViewModel.getFeeds().observe(this, Observer { feeds -> feeds?.apply { feedAdapter.swap(feeds) feedAdapter.notifyDataSetChanged() } }) Now we have a DetailActivity page which updates the feed (item) and Activity is finished but

How to connect ViewModel with Repository so that data is propagated to the View (MVVM, Livedata)

末鹿安然 提交于 2019-12-06 11:09:12
问题 I've added some code to make my question more clear. Retrofit interface: public interface JsonPlaceHolderAPI { public static final String BASE_URL = "https://jsonplaceholder.typicode.com/"; @GET("todos/{number}") Call<ResponseBody> getJsonResponse(@Path("number") String number); } The repository: --> fetchResponse() takes Viewmodel's MutableLiveData as parameter and uses it to update its value and then trigger View to change its UI. public class Repository { private final JsonPlaceHolderAPI

Filtering RecyclerView's list with LiveData content using SearchView

独自空忆成欢 提交于 2019-12-06 09:15:30
问题 I've created RecyclerView which contains simple list of words (String names of GroupVc objects). As list can be very long I want to make it filterable with SearchView in Toolbar. Architecture of my application is based on Android Architecture Components, where all GroupVc objects exist in Room database and access to DataBase from UI is provided through ViewModel and Repository objects. I'm filing my RecyclerView with list of all GroupsVc wrapped in LiveData to keep it updated. My problem is

Getting Initial Value for LiveData Always Returning Null

浪尽此生 提交于 2019-12-06 07:33:40
I am trying to load the loggedInUser from the Local Room Database, when the App starts. I would like to skip prompting user to log-in if the saved Authentication Token of the previously saved user is still valid! So, from the DAO , I want to return a LiveData object containing the previously logged-in user , then observe it for subsequent changes. The challenge I have is that the method to get the currently logged-in user always returns null if I wrap the result inside a LiveData , but it returns the expected user if returned as a POJO . How can I force LiveData to run synchronously just to

LiveData List doesn't update when updating database

筅森魡賤 提交于 2019-12-06 06:52:31
I'm currently refactoring legacy code to use Android Architecture Components and set up a room db and volley requests within a kind of repository pattern. So the presentation/domain layer asks the repository to get LiveData-Objects to observe or tell him to synchronize with the server, after which old db entries are deleted and all current ones refetched from the server. I've written tests for the synchronization part, so I'm sure, that the objects get fetched and inserted to the database correctly. But when writing a test to observe the entries of that db table (and test if the objects were

Using Realm and LiveData. Converting LiveData<RealmResults<CustomModelObject>> to LiveData<List<CustomModelObject>>

浪子不回头ぞ 提交于 2019-12-05 20:41:57
I am trying out Realm along with Android architecture components including LiveData. I have been following Google's Guide to Application Architecture: https://developer.android.com/topic/libraries/architecture/guide.html ...substituting Room with Realm. I have everything working using: LiveData<RealmResults<CustomModelObject>> from my repository layer right through ViewModel to View. I am thinking it might be nicer to only have more generic types coming back from repository so LiveData<List<CustomModelObject>> rather than LiveData<RealmResults<CustomModelObject>> . Here is a code snippet of

Is it possible to enforce non-nullability of LiveData values?

徘徊边缘 提交于 2019-12-05 17:24:54
问题 Is there any way to enforce non-nullability of LiveData values? Default Observer implementation seems to have @Nullable annotation which forces an IDE to suggest that the value might be null and should be checked manually: public interface Observer<T> { /** * Called when the data is changed. * @param t The new data */ void onChanged(@Nullable T t); } 回答1: While there a few things you can do, it is your responsibility to make sure you don't pass null to the LiveData . In addition to that,

NetworkBoundResource helper class without Room

六月ゝ 毕业季﹏ 提交于 2019-12-05 17:16:03
问题 When I tried to implements the NetworkBoundResource and Resource helper class for the Room Db and Retrofit, it works perfect. However, I need to implement the Search Result from RESTful using Retrofit only without Room . The Resources class is good and I dont need to change it. What I want to do is try to remove db source inside this class. public abstract class NetworkBoundResource<ResultType, RequestType> { private final AppExecutors appExecutors; private final MediatorLiveData<Resource

LiveData with shared preferences

左心房为你撑大大i 提交于 2019-12-05 16:55:16
问题 I have a settings screen where I am setting some values. When I set those values it gets saved in shared preferences and these values are needed in my request to the network api call as parameters. Now I can use a listener for shared preferences in my activity then make an api call and get fresh data, but I want to use this with LiveData. How can I listen for changes in the shared preferences using LiveData and then make the network call using the new parameters. 回答1: Java Code by Idish,