title: Regarding loading items to the spinner using volley library
body: <p>I am trying to load items to the spinner inside my <code>MainActivity</code> toolbar.Inside <code>MainActivity</code> there is a fragment called <code>HomeFragment</code>. And that fragment contains <code>SlidingTabLayout</code> and once I click different tabs I need to load different data sets to the above mentioned spinner. </p>
  
  <blockquote>
    <p>Here is how it looks like :</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/tdabG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tdabG.png" alt="enter image description here"></a></p>
  
  <p>I have implemented method inside <code>MainActivity</code> to add data to the spinner :</p>
  
  <pre><code>    public void addItemsToSpinner(final Collection&lt;String&gt; collection) {
  
          ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();
          list.addAll(collection);
  
          CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(getApplicationContext(), list);
          subCategories_spinner.setAdapter(spinAdapter);
          subCategories_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; adapter, View v, int position, long id) {
                  // On selecting a spinner item
                  String item = adapter.getItemAtPosition(position).toString();
  
                  // Showing selected spinner item
  //                Toast.makeText(getApplicationContext(), "Selected  : " + item,
  //                        Toast.LENGTH_SHORT).show();
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                  // TODO Auto-generated method stub
  
              }
          });
  
      }
  </code></pre>
  
  <p>And inside my <code>HomeActivity</code> I have set <code>OnPageChangeLister</code> to <code>SlidingTabLayout</code> like below :</p>
  
  <pre><code>mTabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                  if (firstScrolled) {
                      // Here log-cat gives me I/Position: On page scrolled - 0 which means on stating app this line executed. But data not set to the spinner.
                     // Log.i("Position", "On page scrolled - " + position);
                      volleySubCatFilter("women");
                      ((MainActivity) getActivity()).addItemsToSpinner(subs);//debug console subs (ArrayList) size given as 0 here
                      firstScrolled = false;
                  }
  
  
              }
  
              @Override
              public void onPageSelected(int position) {
                  if (position == 0) {
                     // Log.i("Position", "On page selected - " + position);
                      volleySubCatFilter("women");
                     ((MainActivity) getActivity()).addItemsToSpinner(subs);
  
                  }
                  if (position == 1) {
                     // Log.i("Position", "On page selected - " + position);
                      volleySubCatFilter("men");
                      ((MainActivity) getActivity()).addItemsToSpinner(subs);
  
                  }
                  if (position == 2) {
                     // Log.i("Position", "On page selected - " + position);
                      volleySubCatFilter("household%20goods");
                     ((MainActivity) getActivity()).addItemsToSpinner(subs);
  
                  }
              }
  
              @Override
              public void onPageScrollStateChanged(int state) {
  
              }
          });
  </code></pre>
  
  <p>With use of <code>volleySubCatFilter("category_here");</code> method I retrieve data from volley request to the server and it is working fine. </p>
  
  <p>The problem I have is first time loading the application <strong>WOMEN</strong> tab is selected and but no data loaded to the spinner.But I tried with <code>Log.i("Position", "On page scrolled - " + position);</code> and as soon as the app loaded it gives me <code>I/Position: On page scrolled - 0</code> in my log-cat.But data doesnt load.
  Debuging line <code>((MainActivity) getActivity()).addItemsToSpinner(subs)</code> tells 0 items.Once I press another tab then it starts loading data which belongs to previous tab. Example :- Pressing <strong>MEN</strong> tab right after app loaded getting data belongs to <strong>WOMEN</strong> tab.Then I click on any tab it start loading data belongs to <strong>WOMEN</strong> tab. </p>
  
  <blockquote>
    <p>If I brief this problem data loading to the spinner is one step
    behind.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/7b1Vj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7b1Vj.png" alt="enter image description here"></a></p>
  
  <p>It would be grateful if anyone suggest me a way to get rid of this bug.Thanks in advance.</p>