title: Reusing Fragment and layout.xml for Android Tabs Action Bar
body: <p>I am programming my first Android App and am facing a problem :( I am using Eclipse and the app is intended to run from Android 4.0.3.</p>
  
  <p>I have a tab actionbar on the top of the view, constisting of 4 tabs. Three of them have the same structure:  a radiogroup with two radiobuttons and a listView under it. These tabs are based on the same data and will be used to filter the data. The text of the radioButton depends on the tab and will further filter the data. </p>
  
  <p>The last tab has a completely different layout and function.</p>
  
  <p>I have made two fragment and two layouts (one for the first three, and one for the last tab). The first fragment is managing the radiogroup an the listview. I also have an adapter to set the data model, depending on which tab and button is selected. But somehow I just can't get it to work correctly. The of the radiobuttons doesn't refresh and the list is only showing first time the activity is loaded. No exception is thrown.</p>
  
  <p>Is my approach wrong?</p>
  
  <p>Here is the my main activity:</p>
  
  <pre><code>    @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.list_main);
  
      appContext = getApplicationContext();
  
  
      dataholder = (P1DataHolder) getIntent().getSerializableExtra(
              "data"); //Hold all the data
  
      final ActionBar mainBar = getActionBar();
      mainBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      mainBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
  
      // create the fragments with the name of parent tab
      Fragment aFragment = new ListFragment(
              MyApplication.TAB_A);
      Fragment bFragment = new ListFragment(
              MyApplication.TAB_B);
      Fragment cFragment = new ListFragment(
              MyApplication.TAB_C);
      Fragment settingFragment = new P1SettingFragment();
  
      // Fragment A
      ActionBar.Tab tabA = mainBar.newTab().setText(
              MyApplication.TAB_A);
      tabA.setTabListener(new TabsListener(aFragment));
  
      // Fragment B
      ActionBar.Tab tabB = mainBar.newTab().setText(
              MyApplication.TAB_B);
      tabB.setTabListener(new TabsListener(bFragment));
  
      // Fragment C
      ActionBar.Tab tabC = mainBar.newTab().setText(
              MyApplication.TAB_C);
      tabC.setTabListener(new TabsListener(cFragment));
  
      // Settings
      ActionBar.Tab tabSetting = mainBar.newTab().setText(
              MyApplication.TAB_SETTING);
      tabSetting.setTabListener(new TabsListener(settingFragment));
  
      // Adding Tabs to actionbar
      mainBar.addTab(tabA);
      mainBar.addTab(tabB);
      mainBar.addTab(tabC);
      mainBar.addTab(tabSetting);
  
  }
  </code></pre>
  
  <p>Edit: code of the frame</p>
  
  <pre><code>public class ListFragment extends Fragment {
  
  private ArrayList&lt;Recipe&gt; listAll;
  private ArrayList&lt;Recipe&gt; listNotCookedYet;
  private CustomListView ls;
  private SectionComposerAdapter adapter;
  private Activity parent;
  private String tabTag; // Tab state from Parent
  private View view;
  private SegmentedRadioGroup segmentText;
  private RadioButton rbAll;
  private RadioButton rbNotCookedYet;
  
  
  public ListFragment(String tabTag, ArrayList&lt;Recipe&gt; listAll, ArrayList&lt;Recipe&gt; listNTY) {
      this.tabTag = tabTag;
      this.listAll = listAll;
      this.listNotCookedYet = listNTY;
  }
  
  public String getTabTag(){
      return this.tabTag;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      // Inflate the layout for this fragment
      view = inflater.inflate(R.layout.list_with_section, container,
              false);
      parent = getActivity();
  
      // get Elements
      segmentText = (SegmentedRadioGroup) view
              .findViewById(R.id.segment_text);
      rbAll = (RadioButton) view.findViewById(R.id.button_list_all);
      rbNotCookedYet = (RadioButton) view
              .findViewById(R.id.button_list_notcookedyet);
  
      // Set Content
      if (parent != null &amp;&amp; parent instanceof ListMainActivity) {
  
          ListDataHolder holder = ((ListMainActivity) parent)
                  .getDataHolder();
  
          adapter = new SectionComposerAdapter(parent, holder);
          adapter.getCount();
          initialize(adapter);
  
          System.out.println("ANZAHL ALL: " + listAll.size());
          System.out.println("ANZAHL UNCOOKED: " + listNotCookedYet.size());
  
          //The default text is "all (*)" and "not cooked yet (*)"
          rbAll.setText(rbAll.getText().toString()
                  .replace("*", listAll.size() + ""));
          rbNotCookedYet.setText(rbNotCookedYet.getText().toString()
                  .replace("*", listNotCookedYet.size() + ""));
  
          segmentText
                  .setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
                      @Override
                      public void onCheckedChanged(RadioGroup group,
                              int checkedId) {
  
                          if (rbAll.getId() == checkedId) {
                              adapter.setDataModel(tabTag, true);
                              System.out.println("CHECKED: All");
  
                          }
  
                          else if (rbNotCookedYet.getId() == checkedId) {
                              adapter.setDataModel(tabTag, false);
                              System.out.println("CHECKED: Not Yet");
                          }
  
  
                      }
                  });
  
      }
      return view;
  }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
  }
  
  private void initialize(SectionComposerAdapter adapter) {
      ls = (CustomListView) view
              .findViewById(R.id.list_services);
      ls.setPinnedHeaderView(LayoutInflater.from(parent).inflate(
              R.layout.item_composer_header, ls, false));
      ls.setAdapter(adapter);
      ls.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
  
              Intent intent = new Intent(view.getContext(),
                      RecipeActivity.class);
              startActivityForResult(intent, 0);
  
          }
      });
  }
  </code></pre>
  
  <p>The ListDataHolder has sectioned the data alphabetically and holds them as separate Arraylist for each tab and each radiobutton selection (so there are 6 arraylists).</p>
  
  <p>And here is my adapter:</p>
  
  <pre><code>public class SectionComposerAdapter extends CustomListAdapter {
  
  private Activity activity;
  private ListDataHolder dataholder;
  private static LayoutInflater inflater = null;
  private ImageLoader imageLoader;
  private List&lt;Pair&lt;String, ArrayList&lt;Recipe&gt;&gt;&gt; data;
  
  public SectionComposerAdapter(Activity a, ListDataHolder holder) {
      activity = a;
      this.dataholder = holder;
      inflater = (LayoutInflater) activity
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      imageLoader = new ImageLoader(activity.getApplicationContext());
      data = dataholder.getAllData(dataholder.getAZRecipeAll());
  }
  
  public void setDataModel(String tab, boolean all) {
      if (MyApplication.TAB_A.equals(tab)) {
          if (all) {
              data = dataholder.getAllData(dataholder.getAZRecipeAll());
          } else {
              data = dataholder.getAllData(dataholder
                      .getAZRecipeNotCookedYet());
          }
      } else if (MyApplication.TAB_B.equals(tab)) {
          if (all) {
              data = dataholder.getAllData(dataholder.getMeatRecipeAll());
          } else {
              data = dataholder.getAllData(dataholder
                      .getMeatRecipeNotCookedYet());
          }
      } else if (MyApplication.TAB_C.equals(tab)) {
          if (all) {
              data = dataholder
                      .getAllData(dataholder.getVegetarianRecipeAll());
          } else {
              data = dataholder.getAllData(dataholder
                      .getVegetarianRecipeNotCookedYet());
          }
      }
  
  
  }
  
  @Override
  public int getCount() {
      int res = 0;
      for (int i = 0; i &lt; data.size(); i++) {
          res += data.get(i).second.size();
      }
      return res;
  }
  
  @Override
  public Recipe getItem(int position) {
      int c = 0;
      for (int i = 0; i &lt; data.size(); i++) {
          if (position &gt;= c &amp;&amp; position &lt; c + data.get(i).second.size()) {
              return data.get(i).second.get(position - c);
          }
          c += data.get(i).second.size();
      }
      return null;
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  @Override
  protected void onNextPageRequested(int page) {
  }
  
  @Override
  protected void bindSectionHeader(View view, int position,
          boolean displaySectionHeader) {
      if (displaySectionHeader) {
          view.findViewById(R.id.list_section_header).setVisibility(
                  View.VISIBLE);
          TextView lSectionTitle = (TextView) view
                  .findViewById(R.id.list_section_header);
          lSectionTitle
                  .setText(getSections()[getSectionForPosition(position)]);
      } else {
          TextView lSectionTitle = (TextView) view
                  .findViewById(R.id.list_section_header);
          lSectionTitle
                  .setText(getSections()[getSectionForPosition(position)]);
          view.findViewById(R.id.list_section_header)
                  .setVisibility(View.GONE);
      }
  }
  
  @Override
  public View getAmazingView(int position, View convertView, ViewGroup parent) {
      View vi = convertView;
      if (vi == null)
          vi = inflater.inflate(R.layout.list_row, null);
  
      TextView recipename = (TextView) vi.findViewById(R.id.recipe_name);
      TextView recipevip = (TextView) vi.findViewById(R.id.recipe_fav_mark);
      ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);
  
      Recipe recipe = new Recipe();
      recipe = getItem(position);
  
      // Setting all values in listview
      recipename.setText(recipe.getTitle);
      recipevip.setText(recipe.isFav() ? "FAV" : "");
      imageLoader.DisplayImage(recipe.getImage_thumbnail(), thumb_image);
  
      return vi;
  }
  
  @Override
  public void configurePinnedHeader(View header, int position, int alpha) {
      TextView lSectionHeader = (TextView) header;
      lSectionHeader.setText(getSections()[getSectionForPosition(position)]);
      // lSectionHeader.setBackgroundColor(alpha &lt;&lt; 24 | (0xbbffbb));
      // lSectionHeader.setTextColor(alpha &lt;&lt; 24 | (0x000000));
  }
  
  @Override
  public int getPositionForSection(int section) {
      if (section &lt; 0)
          section = 0;
      if (section &gt;= data.size())
          section = data.size() - 1;
      int c = 0;
      for (int i = 0; i &lt; data.size(); i++) {
          if (section == i) {
              return c;
          }
          c += data.get(i).second.size();
      }
      return 0;
  }
  
  @Override
  public int getSectionForPosition(int position) {
      int c = 0;
      for (int i = 0; i &lt; data.size(); i++) {
          if (position &gt;= c &amp;&amp; position &lt; c + data.get(i).second.size()) {
              return i;
          }
          c += data.get(i).second.size();
      }
      return -1;
  }
  
  @Override
  public String[] getSections() {
      String[] res = new String[data.size()];
      for (int i = 0; i &lt; data.size(); i++) {
          res[i] = data.get(i).first;
      }
      return res;
  }
  </code></pre>
  
  <p>The list works fine without the tabs and radiobuttons. The listener is also working fine, I see the change of tabs and radiobutton in the logs. I just can't get them working together correctly.</p>
  
  <p>Thank you in advance and I am hoping for some tips ;)</p>
  
  <p>Diana</p>