title: Mirror checkboxes state in different fragments with Android
body: <p>I'm developping an app which is supposed to display the list of talks of a conference, and another list with only the papers of the talks.</p>
  
  <p>I posted a few screenshots in this question : <a href="https://stackoverflow.com/questions/20443062/save-checkboxes-states-through-fragments-in-android">Save checkboxes states through fragments in Android</a></p>
  
  <p>My aim is to be able to «mirror» the checkboxes state in the two lists, so that if a paper is checked in the talks list, it also gets checked in the papers list.</p>
  
  <p>Do you have any idea how I can do this?</p>
  
  <p>The lists are implemented using two fragments with their own adapter.</p>
  
  <p>Here's my code :</p>
  
  <p>Main activity :</p>
  
  <pre><code>package be.unamur.confpers;
  public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
  
  private ViewPager viewPager;
  private TabsPagerAdapter mAdapter;
  private ActionBar actionBar;
  SqlHandler sqlHandler;
  // Tab titles
  private String[] tabs = { "Talks", "Papers", "Ma sélection"};
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Initialization database
      sqlHandler = new SqlHandler(this);
  
      viewPager = (ViewPager) findViewById(R.id.pager);
      actionBar = getActionBar();
      mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
  
      viewPager.setAdapter(mAdapter);
      actionBar.setHomeButtonEnabled(false);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);       
  
      // Adding Tabs
      for (String tab_name : tabs) {
          actionBar.addTab(actionBar.newTab().setText(tab_name)
                  .setTabListener(this));
      }
  
      /**
       * on swiping the viewpager make respective tab selected
       * */
      viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
          @Override
          public void onPageSelected(int position) {
              // on changing the page
              // make respected tab selected
              actionBar.setSelectedNavigationItem(position);
          }
  
          @Override
          public void onPageScrolled(int arg0, float arg1, int arg2) {
          }
  
          @Override
          public void onPageScrollStateChanged(int arg0) {
          }
      });
  }
  
  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) {
  }
  
  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {
      // on tab selected
      // show respected fragment view
      viewPager.setCurrentItem(tab.getPosition());
  }
  
  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  }
  
  
  
  }
  </code></pre>
  
  <p>TalksFragment :</p>
  
  <pre><code>public class TalksFragment extends Fragment {
  
  private TalkAdapter talkAdapter;
  List&lt;Talk&gt; talks;
  
  private List&lt;Talk&gt; talksParser(){
  
      try{
          XMLParser parser = new XMLParser ();
          talks = parser.parse(getActivity().getApplicationContext().getAssets().open("talks.xml"));
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      return talks;
  };
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View v = inflater.inflate(R.layout.fragment_talks, container, false);
  
      ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.listTalks);
      talks = talksParser();
      talkAdapter = new TalkAdapter (getActivity(),talks);
      lv.setAdapter(talkAdapter);
  
  
      return v;
  }
  }
  </code></pre>
  
  <p>TalkAdapter :</p>
  
  <pre><code>public class TalkAdapter extends BaseExpandableListAdapter {
  
  private Context context;
  private List&lt;Talk&gt; talks;
  private LayoutInflater inflater;
  private Button changeScreen;
  
  int checked = 0;
  CheckBox cb;
  
  
  
  public TalkAdapter(Context context, 
          List&lt;Talk&gt; talks) { 
      this.context = context;
      this.talks = talks;
      inflater = LayoutInflater.from( context );
  }
  
  public Paper getChild(int groupPosition, int childPosition) {
      return talks.get(groupPosition).getPapers().get(childPosition);
  }
  
  public long getChildId(int groupPosition, int childPosition) {
      return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
  }
  
  private class ViewHolder {
      TextView title;
      TextView author;
      CheckBox name;
  }
  
  
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      View v = null;
      ViewHolder holder = null;
  
      if( convertView == null ){
          v = inflater.inflate(R.layout.child_row, parent, false); 
          Paper p = getChild(groupPosition, childPosition );
  
          cb = (CheckBox)v.findViewById( R.id.check1 );
          //cb.setChecked( p.getState() );
  
          TextView paper = (TextView)v.findViewById(R.id.papername);
          if( paper != null )
              paper.setText( p.getTitle() );
  
          TextView author = (TextView)v.findViewById(R.id.authorname );
          if( author!= null )
              author.setText( p.getAuthor() );
  
          holder = new ViewHolder();
          holder.name = (CheckBox) v.findViewById(R.id.check1);
          holder.title = (TextView) v.findViewById(R.id.papername);
          holder.author= (TextView) v.findViewById(R.id.authorname);
          v.setTag(holder);
  
          holder.name.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  CheckBox cb = (CheckBox) v;
                  Paper paper = (Paper) cb.getTag();
                  String title = paper.getTitle();
                  String author = paper.getAuthor();
                  if (cb.isChecked()){
                      Toast.makeText(context, "Papier "+title+ " ajouté", Toast.LENGTH_SHORT).show();
                      //String query = "INSERT INTO SELECTED_PAPERS(title,author) values ('"
                      //+ title +"','" + author +"')";
                      //sqlHandler.executeQuery(query);
  
                  }
                  if (cb.isChecked()==false){
                      Toast.makeText(context, "Papier "+title+ " retiré", Toast.LENGTH_SHORT).show();
                  }
              }
          });
      }
  
      else{
          v = convertView;
          holder = (ViewHolder) v.getTag();
      }
  
      Paper paper = getChild(groupPosition, childPosition);
      holder.author.setText(paper.getAuthor());
      holder.title.setText(paper.getTitle());
      holder.name.setTag(paper);
  
      return v;
  
  
  }
  
  public int getChildrenCount(int groupPosition) {
      return talks.get(groupPosition).getPapers().size();
  }
  
  public Talk getGroup(int groupPosition) {
      return talks.get(groupPosition);        
  }
  
  public int getGroupCount() {
      return talks.size();
  }
  
  public long getGroupId(int groupPosition) {
      return (long)( groupPosition*1024 );  // To be consistent with getChildId
  } 
  
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      View v = null;
      if( convertView != null )
          v = convertView;
      else
          v = inflater.inflate(R.layout.group_row, parent, false); 
      Talk t = getGroup( groupPosition );
      TextView colorGroup = (TextView)v.findViewById( R.id.papername );
      if( t != null )
          colorGroup.setText( t.getName() );  
  
  
      return v;
  }   
  
  
  
  public boolean hasStableIds() {
      return true;
  }
  
  public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
  } 
  
  public void onGroupCollapsed (int groupPosition) {} 
  public void onGroupExpanded(int groupPosition) {}
  
  
  }
  </code></pre>
  
  <p>PapersFragment :</p>
  
  <pre><code>public class PapersFragment extends Fragment {
  
  private PaperAdapter listAdapter;
  private Context myContext;
  List&lt;Paper&gt; papers = null;
  
  
  
  
  private List&lt;Paper&gt; papersParser () {
  
      try {
          XMLParser2 parser = new XMLParser2();
          papers = parser.parse(getActivity().getApplicationContext().getAssets().open("talks.xml"));
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      return papers;
  };
  
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View v = inflater.inflate(R.layout.fragment_papers, container, false);
  
      ListView lv = (ListView) v.findViewById(R.id.list);
      papers = papersParser();
      listAdapter = new PaperAdapter(getActivity(),papers);
      lv.setAdapter(listAdapter);
      lv.setOnItemClickListener(new OnItemClickListener(){
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                   int position, long id) {
              //Si un jour je veux mettre une description
                     }
      });
  
      if (papers == null) {
      Toast.makeText(getActivity().getApplicationContext(), "Papers vide", Toast.LENGTH_LONG).show();
      }
      return v;
  }
  }
  </code></pre>
  
  <p>PaperAdapter :</p>
  
  <pre><code>public class PaperAdapter extends BaseAdapter implements OnClickListener {
  
  private Context context;
  private List&lt;Paper&gt; papers;
  private LayoutInflater inflater;
  CheckBox cb;
  TextView paper;
  SqlHandler sqlHandler;
  
  public PaperAdapter(Context context, List&lt;Paper&gt; papers) {
      this.context = context;
      this.papers = papers;
      inflater = LayoutInflater.from(context);
  }
  
  @Override
  public int getCount(){
      return papers.size();
  }
  
  @Override
  public Object getItem(int position) {
      return papers.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  private class ViewHolder {
      TextView title;
      TextView author;
      CheckBox name;
  }
  
  @Override
  public View getView(int position, View view, ViewGroup viewGroup) {
  
      ViewHolder holder = null;
  
      // We only create the view if its needed
      if (view == null) {
          view = inflater.inflate(R.layout.child_row, null);
  
          // Set the click listener for the checkbox
          //view.findViewById(R.id.check1).setOnClickListener(this);
  
  
          Paper p = (Paper) getItem(position);
  
          // Set the example text and the state of the checkbox
          CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
          //cb.setChecked(p.isSelected());
          // We tag the data object to retrieve it on the click listener.
  
          paper = (TextView)view.findViewById(R.id.papername);
          if (paper != null)
              paper.setText(p.getTitle());
  
          TextView author = (TextView)view.findViewById(R.id.authorname);
          if( author!= null )
              author.setText( p.getAuthor() );
  
          holder = new ViewHolder();
          holder.name = (CheckBox) view.findViewById(R.id.check1);
          holder.title = (TextView) view.findViewById(R.id.papername);
          holder.author= (TextView) view.findViewById(R.id.authorname);
          view.setTag(holder);
  
          holder.name.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  CheckBox cb = (CheckBox) v;
                  Paper paper = (Paper) cb.getTag();
                  String title = paper.getTitle();
                  String author = paper.getAuthor();
                  if (cb.isChecked()){
                      Toast.makeText(context, "Papier "+title+ " ajouté", Toast.LENGTH_SHORT).show();
                      //String query = "INSERT INTO SELECTED_PAPERS(title,author) values ('"
                              //+ title +"','" + author +"')";
                      //sqlHandler.executeQuery(query);
  
                  }
                  if (cb.isChecked()==false){
                      Toast.makeText(context, "Papier "+title+ " retiré", Toast.LENGTH_SHORT).show();
                  }
              }
          });
      }
      else{
          holder = (ViewHolder) view.getTag();
      }
  
      Paper paper = papers.get(position);
      holder.author.setText(paper.getAuthor());
      holder.title.setText(paper.getTitle());
      holder.name.setTag(paper);
  
      return view;
  }
  
  /*@Override
  /** Will be called when a checkbox has been clicked. */
  public void onClick(View view, int position) {
      /*TextView p = (TextView) view.findViewById(R.id.papername);
      TextView a = (TextView) view.findViewById(R.id.authorname);
      CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
      String title = p.getText().toString();
      String author = a.getText().toString();
      String query = "INSERT INTO SELECTED_PAPERS(title,author) values ('"
              + title +"','" + author +"')";
      sqlHandler.executeQuery(query);*/
      //TextView p = (TextView) view.findViewById(R.id.papername);
      //TextView a = (TextView) view.findViewById(R.id.authorname);
      //String title = p.getText().toString();
      //String author = a.getText().toString();
      Paper p = (Paper) this.getItem(position);
      String title = p.getTitle();
      Toast.makeText(context, "Papier ajouté", Toast.LENGTH_SHORT).show();
  
  }
  
  @Override
  public void onClick(View arg0) {
      // TODO Auto-generated method stub
  
  }
  
  /*
  private void savePrefs(String key, String checked) {
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
      Editor edit = sp.edit();
      edit.putString (key, checked);
      edit.commit();
  
  }*/
  
  
  }
  </code></pre>
  
  <p>Thanks a lot!</p>