Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing an android app that requires multiple tabs and am using a third party plug-in i.e 'com.astuetz:pagerslidingtabstrip:1.0.1' i followed the code sample here
http://stackoverflow.com/questions/26178838/pagerslidingtabstrip-implement-guide[^].

Have been able to implement the code and it is working fine but what if i want to add more tabs up to 4-6, i have tried adding more tabs but am getting errors see the code below

Java
public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return (position == 0)? "Headlines" : "All News" : "Videos" : "Sports" ; //errors here
    }
    @Override
    public int getCount() {
        return 4;
    }
    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return (position == 0)? new FragmentA() : new FragmentB() : new FragmentC() : new FragmentD(); //errors here
    }
}


Kindly help
Posted
Updated 29-Jun-15 0:28am
v2

1 solution

The ternary operator[^] is so-called because it consists of precisely three parts:
<test> ? <true part> : <false part>

You can't add extra parts at the end and expect the compiler to know what you want it to do!

You can combine multiple ternary operators:
<test 1> ? <test 1 true part> : (<test 2> ? <test 2 true part> : <false part>)

However, that isn't particularly easy to read.

A better solution is to us the switch statemet[^]:
Java
@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "Headlines";
        case 1:
            return "All News";
        case 2:
            return "Videos";
        case 3:
            return "Sports";
        default:
            return "# Unknown position #";
    }
}
 
Share this answer
 
Comments
Member 10627743 29-Jun-15 10:08am    
Thank you Richard Deeming it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900