title: How can I populate an adapter in a fragment which is within a viewPager within a dialogFragment?
body: <p>I'm trying to create the following scenario:</p>
  
  <p>User clicks a <code>Button</code> > this shows a <code>DialogFragment</code>
  The <code>DialogFragment</code> contains a <code>ViewPager</code>, which has 2 tabs - one shows a colour picker fragment and the other a symbol picker fragment
  Each picker fragment contains an adapter which transforms an array of colours/symbols into a nice grid.</p>
  
  <p>However, it's currently showing up blank - the <code>DialogFragment</code> appears, with the other parts of the fragment, but no adapter view.</p>
  
  <p>Here's what I have:</p>
  
  <p>The initial activity is called <code>EditActivity</code>.  It has a <code>Button</code>, which, when clicked, calls this function to reveal the <code>DialogFragmentPickers</code>:</p>
  
  <pre><code>public void showPickers() {
          if(mDialogFragmentPickers == null) return;
          mDialogFragmentPickers.setColourSet(mColourSet);
          mDialogFragmentPickers.show(mFragmentManager, "Pickers");
      }
  </code></pre>
  
  <p><code>DialogFragmentPickers</code> is initialised in the <code>onCreate</code> method of <code>EditActivity</code> like this:</p>
  
  <pre><code>mDialogFragmentPickers = new DialogFragmentPickers();
  </code></pre>
  
  <p>The <code>ColourSet</code> being passed in is basically just an array of the colours that are to be shown in the picker. <code>DialogFragmentPickers</code> should then pass it on through to the <code>FragmentColourPicker</code> when it's initialised.</p>
  
  <p>The <code>DialogFragmentPickers</code> class looks like this (I've only shown the parts pertaining to the <code>ColourPicker</code>, but the <code>SymbolPicker</code> works in the same way):</p>
  
  <pre><code>public class DialogFragmentPickers extends DialogFragment {
  
      private PickersPagerAdapter mPagerAdapter;
      private ViewPager           mViewPager;
      private ColourSet           mColourSet;
      private FragmentColourPicker    mColourPicker;
  
      @Override
      public View onCreateView(LayoutInflater inflater,
              ViewGroup container,
              Bundle savedInstanceState) {
  
          // Inflate layout
          View view = inflater.inflate(R.layout.dialog_fragment_pickers, container, false);
  
          mPagerAdapter = new PickersPagerAdapter(getChildFragmentManager());
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = view.findViewById(R.id.pager);
          mViewPager.setAdapter(mPagerAdapter);
  
          return view;
  
      }
  
      public void setColourSet(ColourSet colourSet) {
          mColourSet = colourSet;
          if(mColourPicker == null) getColourPickerFragment();
          else mColourPicker.setColourSet(mColourSet);
      }
  
      private void getColourPickerFragment() {
          if (mPagerAdapter == null) return;
          mColourPicker = (FragmentColourPicker) mPagerAdapter.getItem(0);
          if(mColourSet != null) mColourPicker.setColourSet(mColourSet);
      }
  
      public class PickersPagerAdapter extends FragmentPagerAdapter
      {
  
          PickersPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position) {
              switch(position) {
                  case 0:
                      return new FragmentColourPicker();
                  case 1:
                      return new FragmentSymbolPicker();
                  default:
                      return null;
              }
          }
  
          @Override
          public int getCount() {
              return 2;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              switch (position) {
                  case 0:
                      return getResources().getString(R.string.colours);
                  case 1:
                      return getResources().getString(R.string.symbols);
              }
              return null;
          }
      }
  </code></pre>
  
  <p>The layout file for this contains:</p>
  
  <pre><code>&lt;android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;!-- Current + adjacent page titles...--&gt;
      &lt;android.support.v4.view.PagerTitleStrip
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="top"
          android:background="#33b5e5"
          android:paddingBottom="4dp"
          android:paddingTop="4dp"
          android:textColor="#fff" /&gt;
  &lt;/android.support.v4.view.ViewPager&gt;
  </code></pre>
  
  <p>The first Fragment used in this pager is <code>FragmentColourPicker</code>.  It is defined like this:</p>
  
  <pre><code>public class FragmentColourPicker extends Fragment {
  
      GridView mColourGrid;
      Context mContext;
      ColourSet mColourSet;
      ChartColourAdapter mColourAdapter;
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fragment_colour_picker, container, false);
  
          mColourGrid = view.findViewById(R.id.gridview_colourPicker);
  
          initialiseWithColourSet(); // Returns without doing anything if colourSet is not set up yet
  
          return view;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          mContext = getActivity();
  
      }
  
      @Override
      public void onDestroy(){ super.onDestroy();}
  
      public void setColourSet(ColourSet thisColourSet) {
          mColourSet = thisColourSet;
          initialiseWithColourSet(); // Returns without doing anything if the colourGrid is not set up yet
      }
  
      public void initialiseWithColourSet() {
  
  // Make sure that the colourSet and the colourGrid have both been initialised
          if(mColourSet == null) return;
          if(mColourGrid == null) return;
  
          // Now set up ChartColourAdapter to display colours in grid
          // Initialise currently highlighted colour to default
          // Set on click listener for each colour
          mColourAdapter = new ChartColourAdapter(mContext,
                  R.layout.adapter_pattern_colour_cell_layout,
                  mColourSet.getColours());
          mColourGrid.setAdapter(mColourAdapter);
  
      }
  
  }
  </code></pre>
  
  <p>and the layout file for <code>FragmentColourPicker</code> (fragment_colour_picker.xml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/colour_picker_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/fui_transparent"
      android:orientation="vertical"&gt;
  
      &lt;GridView 
          android:id="@+id/gridview_colourPicker"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorBg"
          android:paddingBottom="@dimen/activity_standard_margin"
          android:columnWidth="@dimen/grid_cell_column_width"
          android:gravity="center"
          android:horizontalSpacing="10dp"
          android:numColumns="auto_fit"
          android:padding="@dimen/activity_small_margin"
          android:stretchMode="columnWidth"
          android:verticalSpacing="10dp" /&gt;
  
      &lt;TextView
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:text="@string/prompt_swipe_for_symbols"
          android:padding="@dimen/activity_small_margin"
          android:background="@color/colorBg"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>When I run all of this, the <code>DialogFragment</code> appears when the button is clicked, and shows the <code>FragmentColourPicker</code>. I can swipe to the <code>FragmentSymbolPicker</code> and back.  However, the only thing shown in the ColourPicker is the <code>TextView</code> which prompts the user to swipe across to see the symbols.  The <code>GridView</code> with the actual colours in it doesn't show at all.</p>
  
  <p>From the debugger, I think the issue is that <code>initialiseWithColourSet</code> only ever seems to have either <code>colourSet</code> OR <code>colourGrid</code> set - never both. This makes it return without attempting to set up the colour adapter.</p>
  
  <p>If <code>initialiseWithColourSet</code> is called from <code>onCreateView</code>, then colourGrid is set, but colourSet is not (since it hasn't been passed in yet). That's reasonable enough.  However, when colourSet is passed in later (from the DialogFragment), colourGrid has gone back to being null, so again the function returns without doing anything.</p>
  
  <p>I clearly have something in the wrong order, or am re-initialising the colourGrid / ColourPicker without realising it.</p>