title: Android ViewPager, ListFragments and updating when an element in a list is changed
body: <p>Im writing my first android app as a coursework for university. So far I have most things working, but I have come across an issue. </p>
  
  <p>The app is structured so I have an Activity that sets up a ViewPager with 3 Fragments in it. Each Fragment is an extension of ListFragment, the list are filled with data pulled from a database. The adapters display custom views for each ListItem, which include a checkbox. </p>
  
  <p>What I want to happen is when the checkbox is ticket all three fragments are updated. That is the database is re-queried for each fragment and the list views get updated. </p>
  
  <p>The HomeActivity. This sets up the ViewPager. It adds the fragments and set up the Fragment Adapter.
      public class HomeActivity extends SherlockFragmentActivity {</p>
  
  <pre><code>    private FragmentAdapter mPagerAdapter;
      private ViewPager mViewPager;
  
      /**
      * Set up the view. Add the Fragments to the viewPager, set up tabs. 
      */
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home);
          getSupportActionBar();
  
          ...
  
          //Set up Fragments. 
          List&lt;MemoFragment&gt; fragments = new Vector&lt;MemoFragment&gt;();
          fragments.add(new IncompleteMemoFragment());
          fragments.add(new CompleteMemoFragment());
          fragments.add(new UrgentMemoFragment());
  
          mPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
  
          mViewPager = (ViewPager) findViewById(R.id.viewPager);
          mViewPager.setAdapter(mPagerAdapter);
          mViewPager.setOnPageChangeListener(new TabListener(mTabHost,mViewPager));
          mViewPager.setOffscreenPageLimit(3);
      }
      ...
  }
  </code></pre>
  
  <p>The Fragment Adapter</p>
  
  <pre><code>public class FragmentAdapter extends FragmentPagerAdapter {
  
      private List&lt;MemoFragment&gt; mFragments;
  
      public FragmentAdapter(FragmentManager fm, List&lt;MemoFragment&gt; fragments) {
          super(fm);
          this.mFragments = fragments;
      }
  
      @Override
      public MemoFragment getItem(int position) {
          return mFragments.get(position);
      }
  
      @Override
      public int getCount() {
          return this.mFragments.size();
      }
  
  }
  </code></pre>
  
  <p>The MemoFragment class, IncompleteMemoFragment, CompleteMemoFragment, UrgentMemoFragment all extend this, and are 99% the same, they just pull a different set of data from the database.  </p>
  
  <pre><code>public class MemoFragment extends SherlockListFragment {
  
      protected DatabaseSource datasource;
  
      /**
       * Creates the View. 
       */
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_memo_list, container, false);
  
          datasource = new DatabaseSource(container.getContext());
          datasource.open();
  
          List&lt;Memo&gt; values = getMemos();
  
          datasource.close();
  
          MemoListViewAdaptor adapter = new MemoListViewAdaptor(container.getContext(), values);
  
          setListAdapter(adapter);
  
          return rootView;
      }
  
      /**
      * Gets the memos to show.
      * @return the memos to show.
      */
      public List&lt;Memo&gt; getMemos(){
          return datasource.getAllMemos();
      }
  
  }
  </code></pre>
  
  <p>This is the MemoListViewAdapter. Here you will see the checkbox im trying to look for changes too.</p>
  
  <pre><code>public class MemoListViewAdaptor extends ArrayAdapter&lt;Memo&gt; {
      private final Context mContext;
      private final List&lt;Memo&gt; mMemos;
  
      /**
       * Save the context and the Memos
       * @param context the context to save
       * @param memos the memos to save.
       */
      public MemoListViewAdaptor(Context context, List&lt;Memo&gt; memos) {
          super(context, R.layout.memo_list_row, memos);
          this.mContext = context;
          this.mMemos = memos;
      }
  
      /**
       * Get the view. Sets up the list to use the custom view. 
       */
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View rowView = inflater.inflate(R.layout.memo_list_row, parent, false);
  
          ...
          CheckBox done = (CheckBox) rowView.findViewById(R.id.done);
  
          Memo memo = mMemos.get(position);
  
  
          done.setOnClickListener(new ClickListener(memo));
          if(memo.getCompleteness() == Memo.COMPLETE){
              done.setChecked(true);
          }
  
          ...
  
          return rowView;
      }
  
  }
  </code></pre>
  
  <p>And, Finally here is the Click listner. </p>
  
  <pre><code>public class ClickListener implements OnClickListener {
      private Memo memo;
  
      ClickListener(Memo memo){
          this.memo = memo;
      }
      @Override
      public void onClick(View v) {
          if(v.getClass() == CheckBox.class){
              Log.v("com.milner.memolist", "These people are crazy. This task is done");
              if(((CheckBox)v).isChecked()){
                  memo.setCompleteness(1);
              }else{
                  memo.setCompleteness(0);
              }
              DatabaseSource datasource = new DatabaseSource(v.getContext());
              datasource.open();
              datasource.updateMemo(memo);
              datasource.close();
  
          }else{
          ...
          }
      }  
  }
  </code></pre>
  
  <p>So, there you have it. Thats what I have so far! As you can see I've probably done this a very complex way... I dont have much experience with android. I guess what I really want to be doing is to reload all the fragments when the check box is ticked.</p>
  
  <p>Is there an easy way to do this? 
  Is there even a complex way to do this? I just cant get my head around this last bit! </p>
  
  <p>Hope you can help! :)</p>
  
  <p>Thanks</p>
  
  <p>Tom M</p>