title: Passing data from Adapter to Item's ViewModel in Android
body: <p>I have a view pager with an adapter that has a number of items fetched from the cloud. I add the pages to the adapter through the fragment.
  I am trying to pass a variable to the Item's view model every time a new page is instantiated from the adapter so as to notify one of the views that depend on this view model. I have tried a few workarounds but nothing has been successful so far. Would anyone please advice how I may pass this data.</p>
  <p>Below is my adapter</p>
  <pre><code>public class HelpPagerAdapter extends PagerAdapter {
  
  private final List&lt;HelpPage&gt; mHelpPages;
  
  private int currentPage = 0;
  
  public HelpPagerAdapter(Context context) {
      mPages = new ArrayList&lt;&gt;();
  }
  
  @Override
  public int getCount() {
      return mHelpPages.size();
  }
  
  @Override
  public boolean isViewFromObject(View view, Object object) {
      return view == object;
  }
  
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((View) object);
  }
  
  @Override
  public Object instantiateItem(ViewGroup container, int position) {
      HelpPage helpPage = mHelpPages.get(position);
      currentPage = position;
  
      LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      ViewDataBinding binding = DataBindingUtil.inflate(inflater, helpPage.getLayout(), container, false);
      binding.setVariable(helpPage.getViewModelBindingId(), helpPage.getViewModel());
  
      container.addView(binding.getRoot());
  
      return binding.getRoot();
  }
  
  public void addPage(@LayoutRes int layout, Object viewModel, int viewModelBindingId) {
      mHelpPages.add(new HelpPage(layout, viewModel, viewModelBindingId));
  }
  
  private class HelpPage {
      private final @LayoutRes int mLayout;
      private final Object mViewModel;
      private final int mViewModelBindingId;
  
      HelpPage(@LayoutRes int layout, Object viewModel, int viewModelBindingId) {
          mLayout = layout;
          mViewModel = viewModel;
          mViewModelBindingId = viewModelBindingId;
      }
  
      @LayoutRes int getLayout() {
          return mLayout;
      }
  
      Object getViewModel() {
          return mViewModel;
      }
  
      int getViewModelBindingId() {
          return mViewModelBindingId;
      }
  }
  }
  </code></pre>
  <p>Part of my fragment code</p>
  <pre><code> @Override
  public void onAttach(Context context) {
      super.onAttach(context);
  
      Bundle bundle = getArguments();
      if (bundle == null || (!bundle.containsKey(ARG_STEPS) &amp;&amp; !bundle.containsKey(ARG_VIEWS))) {
          throw new IllegalStateException(&quot;HelpFragment cannot be initialised without views&quot;);
      }
  
      HelpPagerAdapter adapter = new HelpPagerAdapter(getContext());
      if (bundle.containsKey(ARG_STEPS)) {
          List&lt;Step&gt; steps = Parcels.unwrap(bundle.getParcelable(ARG_STEPS));
          adapter = addHelpPages(steps, adapter);
      }
  
      mHelpViewModel = new HelpViewModel(adapter);
  }
  
  
  private HelpPagerAdapter addHelpPages(List&lt;Step&gt; steps, HelpPagerAdapter adapter) {
      for (Step step : steps) {
          adapter.addPage(R.layout.layout_help, new HelpItemViewModel(step), BR.viewModel);
      }
  
      return adapter;
  }
  </code></pre>
  <p>Part of my ViewModel</p>
  <pre><code>public class HelpItemViewModel {
  
  private final String mText;
  private final String mVideoId;
  private final String mImageUrl;
  
  public HelpItemViewModel(Step step) {
  
      mText = step.getText();
      mHelpId = step.getHelpId();
      mImageUrl = step.getImage();
     
  }
  }
  </code></pre>