How to change a live data instance in view model?How to save an Android Activity state using save instance state?How do I pass data between Activities in Android application?Loadmanager onLoadFinished not calledI am trying to set alarm on specific time using alarm manager but alarm initiated instantly?How to create RecyclerView with multiple view type?View.SurfaceView, why its member, mSurfaceHolder, returns null from getSurface()?Android implement search with view model and live dataRoom with two database 'tables', 'foreign key' and 'live data'Live data observer not triggered after room db queryHow to add child(Product) under a child(Store) in Firebase Database using RecyclerView
Does Swami Vivekananda belong to Shankara parampara?
Confusion in understanding the behavior of inductor in RL circuit with DC source
Searching for single buildings in QGIS
Square wave to sawtooth wave using two BJT
Available snapshots for main net?
How come having a Deathly Hallow is not a big deal?
Non-inverting amplifier ; Single supply ; Bipolar input
Veritaserum used on Harry
How to extract coefficients of a generating function like this one, using a computer?
Can I hire several veteran soldiers to accompany me?
Finding an optimal set without forbidden subsets
Which high-degree derivatives play an essential role?
Is it theoretically possible to hack printer using scanner tray?
German idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)
What is this white film on slides from late 1950s?
Could citing a database like libgen get one into trouble?
Can you run PoE Cat6 alongside standard Cat6 cables?
Simplify the code
What is the meaning of "it" in "as luck would have it"?
Why is the saxophone not common in classical repertoire?
Is my background sufficient to start Quantum Computing
How to model a Coral or Sponge Structure?
How do I use efficient repeats in sheets for pop music?
What is the point of using the kunai?
How to change a live data instance in view model?
How to save an Android Activity state using save instance state?How do I pass data between Activities in Android application?Loadmanager onLoadFinished not calledI am trying to set alarm on specific time using alarm manager but alarm initiated instantly?How to create RecyclerView with multiple view type?View.SurfaceView, why its member, mSurfaceHolder, returns null from getSurface()?Android implement search with view model and live dataRoom with two database 'tables', 'foreign key' and 'live data'Live data observer not triggered after room db queryHow to add child(Product) under a child(Store) in Firebase Database using RecyclerView
I am setting a room query in onCreate() which returns a live data instance that I observe in the following.
viewModel.setmQueryMediaList(null, MediaUtils.DATE_TAKEN_KEY, MediaUtils.DATE_TAKEN_KEY);
viewModel.getmQueryMediaList().observe(MainActivity.this, new Observer<List<MediaStoreData>>()
@Override
public void onChanged(@Nullable List<MediaStoreData> mediaStoreDataList)
List<MediaStoreData> sectionedMediaStoreDataList = MediaUtils.getSectionedList(getBaseContext(), mediaStoreDataList, MediaUtils.ORDER_BY_MONTH );
mRecyclerViewAdapter.submitList(sectionedMediaStoreDataList);
Log.e("MediaDatabase", "something changed! Size:" + (mediaStoreDataList != null ? mediaStoreDataList.size() : 0));
);
onClick()
I want to change the room query and I assumed that the observer triggers on that change but it doesn't.
@Override
public void onAlbumClicked(AlbumData albumData, TextView titleView)
viewModel.setmQueryMediaList(albumData.getDirectory_path(), null, MediaUtils.DATE_TAKEN_KEY);
mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
This is in my ViewModel class
public void setmQueryMediaList(String directory_path, String sectionOrder, String sortOrder)
if(directory_path != null)
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByName();
else
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByDate();
public LiveData<List<MediaStoreData>> getmQueryMediaList()
return mQueryMediaList;
Any ideas what I am doing wrong?
android android-livedata android-jetpack
add a comment |
I am setting a room query in onCreate() which returns a live data instance that I observe in the following.
viewModel.setmQueryMediaList(null, MediaUtils.DATE_TAKEN_KEY, MediaUtils.DATE_TAKEN_KEY);
viewModel.getmQueryMediaList().observe(MainActivity.this, new Observer<List<MediaStoreData>>()
@Override
public void onChanged(@Nullable List<MediaStoreData> mediaStoreDataList)
List<MediaStoreData> sectionedMediaStoreDataList = MediaUtils.getSectionedList(getBaseContext(), mediaStoreDataList, MediaUtils.ORDER_BY_MONTH );
mRecyclerViewAdapter.submitList(sectionedMediaStoreDataList);
Log.e("MediaDatabase", "something changed! Size:" + (mediaStoreDataList != null ? mediaStoreDataList.size() : 0));
);
onClick()
I want to change the room query and I assumed that the observer triggers on that change but it doesn't.
@Override
public void onAlbumClicked(AlbumData albumData, TextView titleView)
viewModel.setmQueryMediaList(albumData.getDirectory_path(), null, MediaUtils.DATE_TAKEN_KEY);
mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
This is in my ViewModel class
public void setmQueryMediaList(String directory_path, String sectionOrder, String sortOrder)
if(directory_path != null)
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByName();
else
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByDate();
public LiveData<List<MediaStoreData>> getmQueryMediaList()
return mQueryMediaList;
Any ideas what I am doing wrong?
android android-livedata android-jetpack
add a comment |
I am setting a room query in onCreate() which returns a live data instance that I observe in the following.
viewModel.setmQueryMediaList(null, MediaUtils.DATE_TAKEN_KEY, MediaUtils.DATE_TAKEN_KEY);
viewModel.getmQueryMediaList().observe(MainActivity.this, new Observer<List<MediaStoreData>>()
@Override
public void onChanged(@Nullable List<MediaStoreData> mediaStoreDataList)
List<MediaStoreData> sectionedMediaStoreDataList = MediaUtils.getSectionedList(getBaseContext(), mediaStoreDataList, MediaUtils.ORDER_BY_MONTH );
mRecyclerViewAdapter.submitList(sectionedMediaStoreDataList);
Log.e("MediaDatabase", "something changed! Size:" + (mediaStoreDataList != null ? mediaStoreDataList.size() : 0));
);
onClick()
I want to change the room query and I assumed that the observer triggers on that change but it doesn't.
@Override
public void onAlbumClicked(AlbumData albumData, TextView titleView)
viewModel.setmQueryMediaList(albumData.getDirectory_path(), null, MediaUtils.DATE_TAKEN_KEY);
mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
This is in my ViewModel class
public void setmQueryMediaList(String directory_path, String sectionOrder, String sortOrder)
if(directory_path != null)
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByName();
else
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByDate();
public LiveData<List<MediaStoreData>> getmQueryMediaList()
return mQueryMediaList;
Any ideas what I am doing wrong?
android android-livedata android-jetpack
I am setting a room query in onCreate() which returns a live data instance that I observe in the following.
viewModel.setmQueryMediaList(null, MediaUtils.DATE_TAKEN_KEY, MediaUtils.DATE_TAKEN_KEY);
viewModel.getmQueryMediaList().observe(MainActivity.this, new Observer<List<MediaStoreData>>()
@Override
public void onChanged(@Nullable List<MediaStoreData> mediaStoreDataList)
List<MediaStoreData> sectionedMediaStoreDataList = MediaUtils.getSectionedList(getBaseContext(), mediaStoreDataList, MediaUtils.ORDER_BY_MONTH );
mRecyclerViewAdapter.submitList(sectionedMediaStoreDataList);
Log.e("MediaDatabase", "something changed! Size:" + (mediaStoreDataList != null ? mediaStoreDataList.size() : 0));
);
onClick()
I want to change the room query and I assumed that the observer triggers on that change but it doesn't.
@Override
public void onAlbumClicked(AlbumData albumData, TextView titleView)
viewModel.setmQueryMediaList(albumData.getDirectory_path(), null, MediaUtils.DATE_TAKEN_KEY);
mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
This is in my ViewModel class
public void setmQueryMediaList(String directory_path, String sectionOrder, String sortOrder)
if(directory_path != null)
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByName();
else
this.mQueryMediaList = mediaDatabase.mediaDao().getAllByDate();
public LiveData<List<MediaStoreData>> getmQueryMediaList()
return mQueryMediaList;
Any ideas what I am doing wrong?
android android-livedata android-jetpack
android android-livedata android-jetpack
edited Mar 31 at 14:52
fabi
asked Mar 25 at 17:33
fabifabi
801 silver badge7 bronze badges
801 silver badge7 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your problem is that you replace the Object which has the Observer attached.
That means that you Obserers are not attached to your new QueryMediaList, so you would need to reset them every time you change the Query.
To do that you could extract your Observer into its own variable, and then reatach that variable to the list, after you changed the query.
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637
– fabi
Mar 26 at 8:59
shouldn'tviewModel.getmQueryMediaList().removeObservers(this);
be:viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?
– glm9637
Mar 26 at 9:06
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
add a comment |
The correct way to do this would be to put the directoryPath
in a MutableLiveData<String>
, then do a Transformations.switchMap
by it to update the LiveData that you're actually observing from your Activity/Fragment.
public void setmQueryMediaList(String directoryPath, String sectionOrder, String sortOrder)
directoryPathLiveData.setValue(directoryPath);
mQueryMediaList = Transformations.switchMap(directoryPathLiveData, (directoryPath) ->
if(directory_path != null)
return mediaDatabase.mediaDao().getAllByName();
else
return mediaDatabase.mediaDao().getAllByDate();
);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55343529%2fhow-to-change-a-live-data-instance-in-view-model%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your problem is that you replace the Object which has the Observer attached.
That means that you Obserers are not attached to your new QueryMediaList, so you would need to reset them every time you change the Query.
To do that you could extract your Observer into its own variable, and then reatach that variable to the list, after you changed the query.
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637
– fabi
Mar 26 at 8:59
shouldn'tviewModel.getmQueryMediaList().removeObservers(this);
be:viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?
– glm9637
Mar 26 at 9:06
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
add a comment |
Your problem is that you replace the Object which has the Observer attached.
That means that you Obserers are not attached to your new QueryMediaList, so you would need to reset them every time you change the Query.
To do that you could extract your Observer into its own variable, and then reatach that variable to the list, after you changed the query.
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637
– fabi
Mar 26 at 8:59
shouldn'tviewModel.getmQueryMediaList().removeObservers(this);
be:viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?
– glm9637
Mar 26 at 9:06
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
add a comment |
Your problem is that you replace the Object which has the Observer attached.
That means that you Obserers are not attached to your new QueryMediaList, so you would need to reset them every time you change the Query.
To do that you could extract your Observer into its own variable, and then reatach that variable to the list, after you changed the query.
Your problem is that you replace the Object which has the Observer attached.
That means that you Obserers are not attached to your new QueryMediaList, so you would need to reset them every time you change the Query.
To do that you could extract your Observer into its own variable, and then reatach that variable to the list, after you changed the query.
answered Mar 25 at 18:16
glm9637glm9637
7296 silver badges17 bronze badges
7296 silver badges17 bronze badges
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637
– fabi
Mar 26 at 8:59
shouldn'tviewModel.getmQueryMediaList().removeObservers(this);
be:viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?
– glm9637
Mar 26 at 9:06
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
add a comment |
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637
– fabi
Mar 26 at 8:59
shouldn'tviewModel.getmQueryMediaList().removeObservers(this);
be:viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?
– glm9637
Mar 26 at 9:06
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637– fabi
Mar 26 at 8:59
private void handleQueryMediaList(String directory_path, String sectionOrder, String sortOrder) if(viewModel.getmQueryMediaList().hasObservers()) viewModel.getmQueryMediaList().removeObservers(this); viewModel.setmQueryMediaList(directory_path, sectionOrder, sortOrder); viewModel.getmQueryMediaList().observe(MainActivity.this, mObserver);
This is how I implemented it, but now the app isn't even launching and is showing no error log....@glm9637– fabi
Mar 26 at 8:59
shouldn't
viewModel.getmQueryMediaList().removeObservers(this);
be: viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?– glm9637
Mar 26 at 9:06
shouldn't
viewModel.getmQueryMediaList().removeObservers(this);
be: viewModel.getmQueryMediaList().removeObservers(mObserver);
? Since you want to remover your observer?– glm9637
Mar 26 at 9:06
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
I tried both. I fixed it. Dumb mistake as I was calling .removeObersers(this) on a null reference... Extracting of the observer into its own variable is the key! Thanks!
– fabi
Mar 26 at 9:11
add a comment |
The correct way to do this would be to put the directoryPath
in a MutableLiveData<String>
, then do a Transformations.switchMap
by it to update the LiveData that you're actually observing from your Activity/Fragment.
public void setmQueryMediaList(String directoryPath, String sectionOrder, String sortOrder)
directoryPathLiveData.setValue(directoryPath);
mQueryMediaList = Transformations.switchMap(directoryPathLiveData, (directoryPath) ->
if(directory_path != null)
return mediaDatabase.mediaDao().getAllByName();
else
return mediaDatabase.mediaDao().getAllByDate();
);
add a comment |
The correct way to do this would be to put the directoryPath
in a MutableLiveData<String>
, then do a Transformations.switchMap
by it to update the LiveData that you're actually observing from your Activity/Fragment.
public void setmQueryMediaList(String directoryPath, String sectionOrder, String sortOrder)
directoryPathLiveData.setValue(directoryPath);
mQueryMediaList = Transformations.switchMap(directoryPathLiveData, (directoryPath) ->
if(directory_path != null)
return mediaDatabase.mediaDao().getAllByName();
else
return mediaDatabase.mediaDao().getAllByDate();
);
add a comment |
The correct way to do this would be to put the directoryPath
in a MutableLiveData<String>
, then do a Transformations.switchMap
by it to update the LiveData that you're actually observing from your Activity/Fragment.
public void setmQueryMediaList(String directoryPath, String sectionOrder, String sortOrder)
directoryPathLiveData.setValue(directoryPath);
mQueryMediaList = Transformations.switchMap(directoryPathLiveData, (directoryPath) ->
if(directory_path != null)
return mediaDatabase.mediaDao().getAllByName();
else
return mediaDatabase.mediaDao().getAllByDate();
);
The correct way to do this would be to put the directoryPath
in a MutableLiveData<String>
, then do a Transformations.switchMap
by it to update the LiveData that you're actually observing from your Activity/Fragment.
public void setmQueryMediaList(String directoryPath, String sectionOrder, String sortOrder)
directoryPathLiveData.setValue(directoryPath);
mQueryMediaList = Transformations.switchMap(directoryPathLiveData, (directoryPath) ->
if(directory_path != null)
return mediaDatabase.mediaDao().getAllByName();
else
return mediaDatabase.mediaDao().getAllByDate();
);
answered Mar 28 at 10:29
EpicPandaForceEpicPandaForce
53k15 gold badges142 silver badges282 bronze badges
53k15 gold badges142 silver badges282 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55343529%2fhow-to-change-a-live-data-instance-in-view-model%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown