Question

I am testing out the FragmentPagerAdapter and I had it all in a single class before. And everything worked, but once I separated SectionsPagerAdapter class, the getString doesn't work under the getPageTitle function.

I know getPageTitle is part of the PagerAdapter class, but I want to know what the best way to have that function included in this class. Do I need to extend the class?

SectionsPageAdapter class

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

// A FragmentPagerAdapter that returns a fragment corresponding to one of the sections/tabs/pages. 
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

@Override  
public Fragment getItem(int position) {  
    // getItem is called to instantiate the fragment for the given page.  
    // Return a DummySectionFragment (defined as a static inner class  
    // below) with the page number as its lone argument.  
    Fragment fragment = new DummySectionFragment();  
    Bundle args = new Bundle();  
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);  
    fragment.setArguments(args);  
    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {  
    Locale l = Locale.getDefault();  
    switch (position) {  
    case 0:  
        return getString(R.string.myFriendsTab).toUpperCase(l);  
    case 1:
        return getString(R.string.myDealsTab).toUpperCase(l);  
    case 2:
        return getString(R.string.featuredDealsTab).toUpperCase(l);
    case 3:
        return getString(R.string.browseCategoriesTab).toUpperCase(l);  
    case 4:
        return getString(R.string.localDealsTab).toUpperCase(l);  
    }  
    return null;  
}

@Override  
public int getCount() {  
     // Show 5 total pages.  
     return 5;  
}
}

MainActivity class

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
    // Fragment PagerAdapter keeps every loaded fragment in memory. 
    // If too memory intensive, switch to FragmentStatePagerAdapter.
    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager; // ViewPager that will host section contents.

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

        // Creates the adapter that will return a fragment for each of the primary sections.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), null);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Was it helpful?

Solution

getString(int) only works for Classes that have access to a Context - Fragments, Activities, etc.

Given that this an Adapter class, it won't have direct access to a Context, so you should probably pass one in with the constructor.

private Context mContext = null;

public SectionsPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}

and then use the member field to access getString(int)

return mContext.getString(R.string.myFriendsTab).toUpperCase(1);

OTHER TIPS

as panini said, getString method need to be called on Context, follow these steps :

  • Step 1 : in the adapter class, create a field to store the Context on it.

    private Context mContext
    
  • Step 2 : in the adapter class, adjust the constructor to pass the Context as the first parameter.

         public SectionsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            mContext = context;
        }
    
  • Step 3 : in the adapter class, and inside the getPageTitle method, call the getString on the Context field mContext,

    @Override
    public CharSequence getPageTitle(int position) {  
    Locale l = Locale.getDefault();  
     switch (position) {  
      case 0:  
        return mContext.getString(R.string.myFriendsTab).toUpperCase(l);  
      case 1:
        return mContext.getString(R.string.myDealsTab).toUpperCase(l);  
      case 2:
        return mContext.getString(R.string.featuredDealsTab).toUpperCase(l);
      case 3:
        return mContext.getString(R.string.browseCategoriesTab).toUpperCase(l);  
      case 4:
        return mContext.getString(R.string.localDealsTab).toUpperCase(l);  
     }  
       return null;  
    }
    
  • Step 4 : wherever you used the adapter adjust it to include the Context parameter as we defined in the constructor.

in MainActivity class, adjust the constructor to be like this :

    mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top