0

i have five columns in my database and five tabs that each column corresponds to each tab
now i want to add tabs to actionbar just if the corresponding column was not empty
so how i should condition tabs to be shown or not depends on their corresponding columns
i used this strategy to condition tabs and it worked but unfortunately the name of tabs are not correct :

public class Content extends FragmentActivity implements ActionBar.TabListener {

private Gcontent g;
private String g_name;
private String g_family;
private String g_about;
private String g_age;
private String g_father;

private ActionBar actionBar;
private ViewPager viewPager;
private TabsPagerAdapter tabsAdapter = new TabsPagerAdapter(getSupportFragmentManager());


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pc_content);

    g = (Gcontent) getIntent().getExtras().get("CurrMember");
    g_name = g.getGname();
    g_family = g.getGfamily();
    g_about = g.getGabout();
    g_age = g.getGstory();
    g_father = g.getGfather();

    viewPager = (ViewPager) findViewById(R.id.tabs_container);
    viewPager.setAdapter(tabsAdapter);

    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setHomeButtonEnabled(false);


    if(!(g_name.equals("empty"))){
        actionBar.addTab(
            actionBar.newTab().
            setText("first tab").
            setTabListener(this)
        );
    }

    if(!(g_family.equals("empty"))){
        actionBar.addTab(
            actionBar.newTab().
            setText("second tab").
            setTabListener(this)
        );
    }

    if(!(g_about.equals("empty"))){
        actionBar.addTab(
            actionBar.newTab().
            setText("third tab").
            setTabListener(this)
        );
    }

    if(!(g_age.equals("empty"))){
        actionBar.addTab(
            actionBar.newTab().
            setText("fourth tab").
            setTabListener(this)
        );
    }

    if(!(g_father.equals("empty"))){
        actionBar.addTab(
            actionBar.newTab().
            setText("fifth tab").
            setTabListener(this)
        );
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

    viewPager.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            viewPager.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}
}

and this is my pager adapter :

public class TabsPagerAdapter extends FragmentPagerAdapter {

int count;

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    switch(index){
    case 0 :
        return new FragmentFirst();
    case 1 :
        return new FragmentSecond();
    case 2 : 
        return new FragmentThird();
    case 3 :
        return new FragmentFourth();
    case 4 :
        return new FragmentFifth();
    }
    return null;
}

@Override
public int getCount() {
    return 5;
}

}
4
  • thats means automaticaly add tab after fetch databse?
    – user4790312
    Commented May 23, 2015 at 18:13
  • whats result of g in g = (Gcontent) getIntent().getExtras().get("CurrMember"); ?
    – user4790312
    Commented May 23, 2015 at 18:27
  • that is a parceable object came from antoher activity by intents Commented May 24, 2015 at 7:45
  • please email me. [email protected]
    – user4790312
    Commented May 24, 2015 at 8:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.