title: ViewPager, FragmentStatePagerAdapter, ListFragment separate list data for each Fragment
body: <p>I have a ViewPager with a dynamic number of pages in it. Each of these pages needs to read from a db and display the dataset for that page. </p>
  
  <p>I compare the currently selected pages title with the first value in the db "username" to check if the data is relevant for that page.</p>
  
  <p>My fragments though dont display the data correctly. Im trying to set an arrayAdapter in the onCreateView, but its not called on the creation of each fragment. In my test case, I have three fragments, user1, user2, user3. onCreateView is called on user1 originally, then is only called again when I swipe back to user2, and never again for user1 or user3.</p>
  
  <p>I need to be able to set the arrayAdapter for each fragment specifically because each one will have a different array of values to display.</p>
  
  <p><strong>PagerAdapter</strong></p>
  
  <pre><code>   public class ExamplePagerAdapter extends FragmentStatePagerAdapter implements TitleProvider{
  
          private Map&lt;Integer, UserFragment&gt; mPageReferenceMap = new HashMap&lt;Integer, UserFragment&gt;();
  
          public ExamplePagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public int getCount() {
              return user;
          }
  
          @Override
          public Fragment getItem(int position) {
              Fragment fragment = new UserFragment();
  
              Bundle args = new Bundle();
              fragment.setArguments(args);        
              return fragment;
          }
  
          @Override
          public String getTitle(int pos) {
              return "@"+users[pos].getName();
          }
  
          @Override
          public void destroyItem(View container, int position, Object object) {              
              mPageReferenceMap.remove(Integer.valueOf(position));
          }
      }
  </code></pre>
  
  <p><strong>UserFragment</strong></p>
  
  <pre><code>public class UserFragment extends ListFragment {
        Context ctx;
        View v;
        Button mAddEntry;
        static int p; 
        static ArrayList&lt;String&gt; entryArray;
  
         @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              ctx = container.getContext();
              if (v != null) 
                  return v;
  
              v = inflater.inflate(R.layout.user_fragment, container, false);
              mAddEntry = (Button) v.findViewById(R.id.entry_b);
              mAddEntry.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Bundle b = new Bundle();
                      b.putInt("pos", p);
                      Intent mi = new Intent(v.getContext(), EntryAdd.class);
                      mi.putExtras(b);
                      startActivity(mi);
                  }
              });             
              updateUserFrag();
              return v;      
         }     
  
         public void updateUserFrag () {         
               entryArray = new ArrayList&lt;String&gt;();
               StringBuilder sb = new StringBuilder();
               final DBAdapter db = new DBAdapter(ctx);
               db.open();
               Cursor c = db.getAllEntries();
               try {
                  while (c.moveToNext()) {
                      if (c.getString(0).equals(MainActivity.users[p].getName())) {
                          sb.append(c.getString(1));
                          entryArray.add(sb.toString());
                      }
                  }
               } catch (Exception e) {}
               c.close();
               db.close();
  
               ArrayAdapter&lt;String&gt; a = new ArrayAdapter&lt;String&gt;(ctx, android.R.layout.simple_list_item_1, entryArray);
               setListAdapter(a);
         }
  
  }
  </code></pre>
  
  <p>Basically, I just need a method I can use to give each fragment its data when its created. onCreateView() doesnt get called for each fragment, so thats no use for me.</p>
  
  <p><strong>UPDATE</strong>
  I think I've almost got it, but I cant figure out this last part. Im using TitlePageIndicators onPageSelected method to send data to my fragment handler.</p>
  
  <pre><code>   TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
      indicator.setViewPager(pager, 0);
      indicator.setOnPageChangeListener(new OnPageChangeListener() {
           @Override
           public void onPageSelected(int position) {
                Message msg = UserFragment.handy.obtainMessage(); 
                Bundle b = new Bundle(); 
                b.putInt("m", 0); 
                b.putInt("p", position); 
                b.putStringArrayList("e", updateUserFrag(position));
                msg.setData(b); 
                UserFragment.handy.handleMessage(msg);                 
           }
  </code></pre>
  
  <p>Handler</p>
  
  <pre><code>   static Handler handy = new Handler() { 
          public void handleMessage(Message msg) { 
              int p = msg.getData().getInt("p"); 
              int m = msg.getData().getInt("m"); 
              ArrayList&lt;String&gt; e = msg.getData().getStringArrayList("e");
  
              switch (m) { 
                  case 0: 
                      Log.d("HANDLER", Integer.toString(p));
                      for (int i=0;i&lt;e.size();i++) {
                          //Log.d("ENTRY", e.get(i));
                      }
                  break; 
               } 
          } 
     }; 
  </code></pre>
  
  <p>The problem is that by the time the handler is called, the Arrayadapter has already been set, and I can find a way to trigger a refresh of that data.</p>