Using an ArrayAdapter with LiveData.











up vote
2
down vote

favorite












I'm familiar with MVVM in WPF, but am in the process of porting an existing Android app to make use of a ViewModel class and LiveData for the first time.



The app "as/was" has a custom ArrayAdapter that is used to correctly display the items contained in a List in a gridview. The code that does that looks essentially like this ("essentially" because there are actually 4 such grids in the activity that all do it like this):



ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);

gridView.setAdapter(playerAdapter)


and when the playerList contents changed, there was an explicit call to



playerAdapter.notifyDataSetChanged()


Since my goal is to have the grid respond automatically when the playerList changes, and never have to notify anything, it seems now that the "playerList" needs to become a MutableLiveData<List<String>>.



I suspect I am going to have a problem though-



The MyAdapter class extends ArrayAdapter<T>



So in practice it's going to become an



ArrayAdapter<MutableLiveData<List<String>>>


But a MutableLiveData isn't going to work there, right?



I could unwrap it and make the call to create it look like this:




ArrayAdapter<String> playerAdapter = 
new MyAdapter<String>(this, R.layout.grid_item, playerList.getValue());



Is this correct? It seems the way of madness.



Ultimately my goal is to just assign the gridView's android:adapter property to this thing in xml and never have to think about it anymore (beyond the required calls to setvalue() and postValue() when I modify the underlying list...which I'm not even sure how would work here.



I would hope there's already a MutableLiveData Collection that's ready to go somewhere and I'd use that.



What the standard way to do this?










share|improve this question


























    up vote
    2
    down vote

    favorite












    I'm familiar with MVVM in WPF, but am in the process of porting an existing Android app to make use of a ViewModel class and LiveData for the first time.



    The app "as/was" has a custom ArrayAdapter that is used to correctly display the items contained in a List in a gridview. The code that does that looks essentially like this ("essentially" because there are actually 4 such grids in the activity that all do it like this):



    ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);

    gridView.setAdapter(playerAdapter)


    and when the playerList contents changed, there was an explicit call to



    playerAdapter.notifyDataSetChanged()


    Since my goal is to have the grid respond automatically when the playerList changes, and never have to notify anything, it seems now that the "playerList" needs to become a MutableLiveData<List<String>>.



    I suspect I am going to have a problem though-



    The MyAdapter class extends ArrayAdapter<T>



    So in practice it's going to become an



    ArrayAdapter<MutableLiveData<List<String>>>


    But a MutableLiveData isn't going to work there, right?



    I could unwrap it and make the call to create it look like this:




    ArrayAdapter<String> playerAdapter = 
    new MyAdapter<String>(this, R.layout.grid_item, playerList.getValue());



    Is this correct? It seems the way of madness.



    Ultimately my goal is to just assign the gridView's android:adapter property to this thing in xml and never have to think about it anymore (beyond the required calls to setvalue() and postValue() when I modify the underlying list...which I'm not even sure how would work here.



    I would hope there's already a MutableLiveData Collection that's ready to go somewhere and I'd use that.



    What the standard way to do this?










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm familiar with MVVM in WPF, but am in the process of porting an existing Android app to make use of a ViewModel class and LiveData for the first time.



      The app "as/was" has a custom ArrayAdapter that is used to correctly display the items contained in a List in a gridview. The code that does that looks essentially like this ("essentially" because there are actually 4 such grids in the activity that all do it like this):



      ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);

      gridView.setAdapter(playerAdapter)


      and when the playerList contents changed, there was an explicit call to



      playerAdapter.notifyDataSetChanged()


      Since my goal is to have the grid respond automatically when the playerList changes, and never have to notify anything, it seems now that the "playerList" needs to become a MutableLiveData<List<String>>.



      I suspect I am going to have a problem though-



      The MyAdapter class extends ArrayAdapter<T>



      So in practice it's going to become an



      ArrayAdapter<MutableLiveData<List<String>>>


      But a MutableLiveData isn't going to work there, right?



      I could unwrap it and make the call to create it look like this:




      ArrayAdapter<String> playerAdapter = 
      new MyAdapter<String>(this, R.layout.grid_item, playerList.getValue());



      Is this correct? It seems the way of madness.



      Ultimately my goal is to just assign the gridView's android:adapter property to this thing in xml and never have to think about it anymore (beyond the required calls to setvalue() and postValue() when I modify the underlying list...which I'm not even sure how would work here.



      I would hope there's already a MutableLiveData Collection that's ready to go somewhere and I'd use that.



      What the standard way to do this?










      share|improve this question













      I'm familiar with MVVM in WPF, but am in the process of porting an existing Android app to make use of a ViewModel class and LiveData for the first time.



      The app "as/was" has a custom ArrayAdapter that is used to correctly display the items contained in a List in a gridview. The code that does that looks essentially like this ("essentially" because there are actually 4 such grids in the activity that all do it like this):



      ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);

      gridView.setAdapter(playerAdapter)


      and when the playerList contents changed, there was an explicit call to



      playerAdapter.notifyDataSetChanged()


      Since my goal is to have the grid respond automatically when the playerList changes, and never have to notify anything, it seems now that the "playerList" needs to become a MutableLiveData<List<String>>.



      I suspect I am going to have a problem though-



      The MyAdapter class extends ArrayAdapter<T>



      So in practice it's going to become an



      ArrayAdapter<MutableLiveData<List<String>>>


      But a MutableLiveData isn't going to work there, right?



      I could unwrap it and make the call to create it look like this:




      ArrayAdapter<String> playerAdapter = 
      new MyAdapter<String>(this, R.layout.grid_item, playerList.getValue());



      Is this correct? It seems the way of madness.



      Ultimately my goal is to just assign the gridView's android:adapter property to this thing in xml and never have to think about it anymore (beyond the required calls to setvalue() and postValue() when I modify the underlying list...which I'm not even sure how would work here.



      I would hope there's already a MutableLiveData Collection that's ready to go somewhere and I'd use that.



      What the standard way to do this?







      android android-livedata mutablelivedata






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 8:51









      JoeHz

      1,74711223




      1,74711223
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I believe the way you are thinking is wrong. Don't worry about this:



          ArrayAdapter<MutableLiveData<List<String>>>


          You will get a list thats all. MutableLiveData is to set a livedata object like setValue() or postValue() manually. Just set your below code whenever liveData gets triggered and gives you the new playerList, something like this:



          viewModel.getPlayListObservable().observe(this, new Observer<List<String>>() {
          @Override
          public void onChanged(List<String> playerList) {
          ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);
          gridView.setAdapter(playerAdapter);
          playerAdapter.notifydataChanged();
          }
          });





          share|improve this answer























          • So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
            – JoeHz
            Nov 21 at 3:55










          • I think I got it. Much thanks!
            – JoeHz
            Nov 21 at 4:21










          • I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
            – JoeHz
            Nov 21 at 8:16












          • yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
            – Prashanth Verma
            Nov 21 at 8:41













          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',
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389285%2fusing-an-arrayadapter-with-livedata%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          I believe the way you are thinking is wrong. Don't worry about this:



          ArrayAdapter<MutableLiveData<List<String>>>


          You will get a list thats all. MutableLiveData is to set a livedata object like setValue() or postValue() manually. Just set your below code whenever liveData gets triggered and gives you the new playerList, something like this:



          viewModel.getPlayListObservable().observe(this, new Observer<List<String>>() {
          @Override
          public void onChanged(List<String> playerList) {
          ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);
          gridView.setAdapter(playerAdapter);
          playerAdapter.notifydataChanged();
          }
          });





          share|improve this answer























          • So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
            – JoeHz
            Nov 21 at 3:55










          • I think I got it. Much thanks!
            – JoeHz
            Nov 21 at 4:21










          • I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
            – JoeHz
            Nov 21 at 8:16












          • yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
            – Prashanth Verma
            Nov 21 at 8:41

















          up vote
          1
          down vote



          accepted










          I believe the way you are thinking is wrong. Don't worry about this:



          ArrayAdapter<MutableLiveData<List<String>>>


          You will get a list thats all. MutableLiveData is to set a livedata object like setValue() or postValue() manually. Just set your below code whenever liveData gets triggered and gives you the new playerList, something like this:



          viewModel.getPlayListObservable().observe(this, new Observer<List<String>>() {
          @Override
          public void onChanged(List<String> playerList) {
          ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);
          gridView.setAdapter(playerAdapter);
          playerAdapter.notifydataChanged();
          }
          });





          share|improve this answer























          • So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
            – JoeHz
            Nov 21 at 3:55










          • I think I got it. Much thanks!
            – JoeHz
            Nov 21 at 4:21










          • I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
            – JoeHz
            Nov 21 at 8:16












          • yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
            – Prashanth Verma
            Nov 21 at 8:41















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I believe the way you are thinking is wrong. Don't worry about this:



          ArrayAdapter<MutableLiveData<List<String>>>


          You will get a list thats all. MutableLiveData is to set a livedata object like setValue() or postValue() manually. Just set your below code whenever liveData gets triggered and gives you the new playerList, something like this:



          viewModel.getPlayListObservable().observe(this, new Observer<List<String>>() {
          @Override
          public void onChanged(List<String> playerList) {
          ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);
          gridView.setAdapter(playerAdapter);
          playerAdapter.notifydataChanged();
          }
          });





          share|improve this answer














          I believe the way you are thinking is wrong. Don't worry about this:



          ArrayAdapter<MutableLiveData<List<String>>>


          You will get a list thats all. MutableLiveData is to set a livedata object like setValue() or postValue() manually. Just set your below code whenever liveData gets triggered and gives you the new playerList, something like this:



          viewModel.getPlayListObservable().observe(this, new Observer<List<String>>() {
          @Override
          public void onChanged(List<String> playerList) {
          ArrayAdapter<String> playerAdapter = new MyAdapter<String>(this, R.layout.grid_item, playerList);
          gridView.setAdapter(playerAdapter);
          playerAdapter.notifydataChanged();
          }
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 8:42

























          answered Nov 20 at 10:55









          Prashanth Verma

          46927




          46927












          • So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
            – JoeHz
            Nov 21 at 3:55










          • I think I got it. Much thanks!
            – JoeHz
            Nov 21 at 4:21










          • I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
            – JoeHz
            Nov 21 at 8:16












          • yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
            – Prashanth Verma
            Nov 21 at 8:41




















          • So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
            – JoeHz
            Nov 21 at 3:55










          • I think I got it. Much thanks!
            – JoeHz
            Nov 21 at 4:21










          • I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
            – JoeHz
            Nov 21 at 8:16












          • yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
            – Prashanth Verma
            Nov 21 at 8:41


















          So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
          – JoeHz
          Nov 21 at 3:55




          So to confirm I get this: PlayerListObservable would be the MutableLiveData of the List<String> and I'd have a method observe changes to that which would set the contents of the GridView? So the GridView and Mutable List<String> actually are unaware of one another?
          – JoeHz
          Nov 21 at 3:55












          I think I got it. Much thanks!
          – JoeHz
          Nov 21 at 4:21




          I think I got it. Much thanks!
          – JoeHz
          Nov 21 at 4:21












          I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
          – JoeHz
          Nov 21 at 8:16






          I believe that the last line should be playerAdapter.notifyDataSetChanged(); no?
          – JoeHz
          Nov 21 at 8:16














          yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
          – Prashanth Verma
          Nov 21 at 8:41






          yes..i will change it. Actually we dont need that as you are setting the adapter in before line. Regarding your 1st comment, yes GridView and Mutable List<String> are unaware of each other as they will be in different layers (if you want to follow architectural pattern). Mutable List<String> will be in ViewModel where as GridView is in View.
          – Prashanth Verma
          Nov 21 at 8:41




















          draft saved

          draft discarded




















































          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389285%2fusing-an-arrayadapter-with-livedata%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen