android-livedata

Refresh data from a network using LiveData

£可爱£侵袭症+ 提交于 2019-12-05 12:43:23
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 is a network connection. The issue is when I click the button, two network calls are triggered, one

MutableLiveData is null in JUnitTest

亡梦爱人 提交于 2019-12-05 05:26:29
I want to write a unit test. Therefore I need MutableLiveData. I started with a very basic test for setup but I cannot instantiate a MutableLiveData object. I is always null when I run the test. Do I have to mock anything? Any suggestions? @RunWith(MockitoJUnitRunner.class) public class DefaultLiveDataTest { private static final int EXPECTED = 5; private final MutableLiveData<Integer> underTest = new MutableLiveData<>(); @Test public void exampleTest() { underTest.setValue(EXPECTED); //underTest is Null assertEquals(underTest.getValue().intValue(), EXPECTED); } } java.lang.NullPointerException

Why this Android Room insert does not work?

▼魔方 西西 提交于 2019-12-05 04:26:14
I implemented caching with room but for some reason it got spoiled, either it does not insert or does not get the data, I've done lot of debugging though, but still no any clue... Can someone help with this? So, the picture is following: MainActivity: mArticleViewModel = ViewModelProviders.of(this).get(ArticleViewModel.class); mArticleViewModel.getArticleList(mPageNumber).observe(this, articles -> { /* doesn't matter */ }); ViewModel: public class ArticleViewModel extends AndroidViewModel { public static final String TAG = "ArticleViewModel"; private LiveData<List<Article>> articleList;

How to convert a List<Object> to PagedList<Object> and vice-versa?

ぐ巨炮叔叔 提交于 2019-12-04 23:06:48
PagedList<Object> is used for Android's cool paging library. To make the question as minimal as possible : If i have a list of strings like List<String> stringList; // it consists of 200 strings I want to convert stringList to type PagedList<String> like PagedList<String> pagedStringList; And also if i have a PagedList<Object> how can convert it to a List<Object> ? I went through this for reference If i try the other way round .... How can I convert List<Object> into DataSource.Factory<Integer, Object> ..so that indirectly i can convert it into PagedList<> ? From DataSource.Factory<Integer,

How to combine two live data one after the other?

半腔热情 提交于 2019-12-04 18:38:00
问题 I have next use case: User comes to registration form, enters name, email and password and clicks on register button. After that system needs to check if email is taken or not and based on that show error message or create new user... I am trying to do that using Room, ViewModel and LiveData. This is some project that on which I try to learn these components and I do not have remote api, I will store everything in local database So I have these classes: RegisterActivity RegisterViewModel User

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

早过忘川 提交于 2019-12-04 16:18:54
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 api; public Repository() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .build(); api =

Android ViewModel inside Service (Alternative)

丶灬走出姿态 提交于 2019-12-04 10:32:12
问题 I have a service which provides UI that is visible to user most of the time. I was experimenting with new Application Architecture when I came with a problem. MyModelviewModel viewModel = ViewModelProviders.of(this).get(MyModelviewModel.class); But as you know this can be only AppCompat or Fragment Is there some alternative? or can I put observer directly on my LiveData like Im puting on ViewModel viewModel.getList().observe(Playground.this, new Observer<List<TestEntity>>() { @Override public

update RecyclerView with Android LiveData

戏子无情 提交于 2019-12-04 07:50:06
问题 There are many examples how to push new list to adapter on LiveData change. I'm trying to update one row (e.g number of comments for post) in the huge list. It would be stupid to reset whole list to change only one field. I am able to add observer onBindViewHolder, but I can't understand when should I remove observer @Override public void onBindViewHolder(ViewHolder vh, int position) { Post post = getPost(position); vh.itemView.setTag(post); post.getLiveName().observeForever(vh.nameObserver);

Why does my activity doesn't see an observed object change?[SOLVED]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 05:47:58
问题 I'm new to Android development and i am trying to understand Live Data with MVVM architecture. I am trying to make the main activity recognize when there is a change in an object that belong to the view-model of the activity. I have created a simple login activity that takes the text from the username and password fields and passes them to the view-model's login function, then the function sends the data to the users repository and then it makes a POST request toa spring-server that is

Convert LiveData to MutableLiveData

我的梦境 提交于 2019-12-04 04:41:46
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() } override fun loadMutableProfileLiveData(): MutableLiveData<Profile> = mutableLiveData inner class