title: how to use Architecture Components ViewModel inside RecyclerView Adapter?
body: <p>I have multiple ViewHolders that work as separated views inside a vertical RecyclerView. I'm practicing with the new Architecture Components ViewModel.</p>
  
  <p>Inside my ViewModel I have a couple of MutableLiveData lists that i want to observe, but how can I call</p>
  
  <pre><code>ViewModelProviders.of((AppCompatActivity)getActivity()).get(FilterViewModel.class)
  </code></pre>
  
  <p>and </p>
  
  <pre><code>mFilterViewModel.getCountries().observe(this, new Observer&lt;ArrayList&lt;TagModel&gt;&gt;() {
              @Override
              public void onChanged(@Nullable ArrayList&lt;TagModel&gt; tagModels) {
  
              }
          });
  </code></pre>
  
  <p>without leaking the activity or save the activity inside the adapter?</p>
  
  <p>my ViewModel</p>
  
  <pre><code>public class FilterViewModel extends ViewModel {
  
  private final MutableLiveData&lt;ArrayList&lt;TagModel&gt;&gt; mCountries;
  private final MutableLiveData&lt;ArrayList&lt;TagModel&gt;&gt; mSelectedCountryProvinceList;
  private final MutableLiveData&lt;ArrayList&lt;TagModel&gt;&gt; mDistanceList;
  
  public FilterViewModel(){
  
      mCountries = new MutableLiveData&lt;&gt;();
      mSelectedCountryProvinceList = new MutableLiveData&lt;&gt;();
      mDistanceList = new MutableLiveData&lt;&gt;();
  
      TagStore.getInstance().subscribe(new StoreObserver&lt;TagSearchList&gt;() {
          @Override
          public void update(TagSearchList object) {
              mCountries.setValue(object.getCountries());
          }
  
          @Override
          public void update(int id, TagSearchList object) {
              if (id == 5){
                  TagStore.getInstance().unSubcribe(this);
                  update(object);
              }
          }
  
          @Override
          public void error(String error) {
  
          }
      }).get(5,"parent");
  
      TagStore.getInstance().subscribe(new StoreObserver&lt;TagSearchList&gt;() {
          @Override
          public void update(TagSearchList object) {
              mSelectedCountryProvinceList.setValue(object.toList());
          }
  
          @Override
          public void update(int id, TagSearchList object) {
              if (id == 6){
                  TagStore.getInstance().unSubcribe(this);
                  update(object);
              }
          }
  
          @Override
          public void error(String error) {
  
          }
      }).get(6,"parent");
  
      TagStore.getInstance().subscribe(new StoreObserver&lt;TagSearchList&gt;() {
          @Override
          public void update(TagSearchList object) {
              mDistanceList.setValue(object.toList());
          }
  
          @Override
          public void update(int id, TagSearchList object) {
              if (id == 51){
                  TagStore.getInstance().unSubcribe(this);
                  update(object);
              }
          }
  
          @Override
          public void error(String error) {
  
          }
      }).get(51,"parent");
  
  }
  
  public void selectCountry(final TagModel country){
      TagStore.getInstance().subscribe(new StoreObserver&lt;TagSearchList&gt;() {
          @Override
          public void update(TagSearchList object) {
              mSelectedCountryProvinceList.setValue(object.toList());
          }
  
          @Override
          public void update(int id, TagSearchList object) {
              if (id == country.getId()){
                  TagStore.getInstance().unSubcribe(this);
                  update(object);
              }
          }
  
          @Override
          public void error(String error) {
  
          }
      }).get(country.getId(),"parent");
  }
  
  public LiveData&lt;ArrayList&lt;TagModel&gt;&gt; getCountries(){
      return mCountries;
  }
  
  public LiveData&lt;ArrayList&lt;TagModel&gt;&gt; getDistances(){
      return mDistanceList;
  }
  
  public LiveData&lt;ArrayList&lt;TagModel&gt;&gt; getProvinces(){
      return mSelectedCountryProvinceList;
  }
  </code></pre>