mercredi 9 mai 2018

Simple Java/Android if statement configuration Help/SectionPageAdapter displaying info

Ok, so in part of the program that I am constructing, there is an Activity that receives and displays ListFragments in a ListView. This Activity also has a ViewPager (that allows for swiping) that Overrides SectionPageAdapter's getItem method in order to return the different fragments that ultimately make up the 5 different swipe-able sections in the Activity.

Now that you have wasted a minute of your life reading the background information, =) here is my actual problem: I am relatively new to programming in general so I apologize if this is a simple question but I am trying to think of a design that consists of only 1 Activity, and 2 fragments (possibly 3).

the getItem override returns a fragment and that what is displayed on each of the 5 tabs. Two fragments will utilize this activity's layout and I would like to somehow be able to make a conditional statement that will decipher which one is appropriate in whihc situation...Here is my code...

enter code hereimport android.graphics.Color; enter code hereimport android.support.design.widget.TabLayout; enter code hereimport android.support.v4.app.Fragment; enter code hereimport android.support.v4.app.FragmentManager; enter code hereimport android.support.v4.app.FragmentPagerAdapter; enter code hereimport android.support.v4.view.ViewPager; enter code hereimport android.support.v7.app.AppCompatActivity; enter code hereimport android.os.Bundle; enter code hereimport android.support.v7.widget.Toolbar; enter code hereimport android.view.Menu;

public class SchResRanActivity extends AppCompatActivity {
public boolean isRanking;
public int daysPassed;

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

    //Toolbar schResRanBar = findViewById(R.id.theTopBar);
    //setSupportActionBar(schResRanBar);

    SectionPagerAdapter sectionPagerAdapter =
            new SectionPagerAdapter(getSupportFragmentManager());

    ViewPager pager = findViewById(R.id.pager);
    pager.setAdapter(sectionPagerAdapter);

    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(pager);
    //tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_schresran_bar, menu);

    return super.onCreateOptionsMenu(menu);
}

void setPageTitle(){}

private class SectionPagerAdapter extends FragmentPagerAdapter {

    private SectionPagerAdapter(FragmentManager fm) { super(fm); }

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

@Override
public CharSequence getPageTitle(int position) {
    switch(position){
        case 0:
            return getResources().getString(R.string.day1);
        case 1:
            return getResources().getString(R.string.day2);
        case 2:
            return getResources().getString(R.string.day3);
        case 3:
            return getResources().getString(R.string.day4);
        case 4:
            return getResources().getString(R.string.day5);
        case 5:
            return getResources().getString(R.string.dayPlayoffs);
    }
    return null;
}
//this code depicts what I am trying to do but is way to clunky! please 
help!
@Override
    public Fragment getItem(int position) {
       if (isRanking) {
        switch (position){
                case 0:
                     if (daysPassed == 1) {
                    return new RankingsFragment();
                      } else {
                    return new NAFragment();
                case 1:
                       if (daysPassed == 2) {
                    return new RankingsFragment();
                      } else {
                    return new NAFragment();
                case 2:
                     if (daysPassed == 3) {
                    return new RankingsFragment();
                      } else {
                    return new NAFragment();                    case 3:
                case 4:
                     if (daysPassed == 4) {
                    return new RankingsFragment();
                      } else {
                    return new NAFragment();
                case 5:
                     if (daysPassed == 5) {
                    return new RankingsFragment();
                      } else {
                    return new NAFragment(); 
                    } 
                  return null;

             if (isRanking == false) {
             case 0:
                     if (daysPassed == 1) {
                    return new ScheduleFragment();
                      } else {
                    return new NAFragment();
                case 1:
                       if (daysPassed == 2) {
                    return new ScheduleFragment();
                      } else {
                    return new NAFragment();
                case 2:
                     if (daysPassed == 3) {
                    return new ScheduleFragment();
                      } else {
                    return new NAFragment();                    case 3:
                case 4:
                     if (daysPassed == 4) {
                    return new ScheduleFragment();
                      } else {
                    return new NAFragment();
                case 5:
                     if (daysPassed == 5) {
                    return new ScheduleFragment();
                      } else {
                    return new NAFragment(); 
                    } 
        return null;
    }

enter code here} enter code here}

//this code depicts what I am trying to do but is way to clunky! can someone suggest a more efficient way to write this?

pretend that there is a button called Rankings and a button called Schedule. the onClick of both of these buttons both bring you to SchResRan activity ,but if schedule button is pressed the variable isRanking == false and each day there is different information dispayed. so if days passed == 0 no info is displayed on any page (all pages will be NA fragments) but if days passed == 1 the day 1 page will display day 1's info if days passed == 2 the day 1 and day 2 page will display the appropriate info so and so forth same for the rankings pages.

thank you so much for your helP! please dont downvote me! lol

Aucun commentaire:

Enregistrer un commentaire