I try to populate tab titles in FragmentPagerAdapter by strings from string.xml. I check a few different ways to do that based on StackOverlow or another sources. Below I present my newest version of code. I still have message that "Cannot Resolve method 'getResources()'. Is it a possibility to correlate tab names with string.xml?
import android.content.Context;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT=5;
private int[] tabTitles = new int[] {R.string.head_character, R.string.head_training, R.string.head_evolutions, R.string.head_talismans, R.string.head_enchantments};
public SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new TrainingFragment();
} else {
return new EvoFragment();
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return getResources().getString(tabTitles[position]);
}
}
try to set context
as a parameter in your Class contractor
and use it in your getPageTitle()
methode ;
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
private Context context;
final int PAGE_COUNT=5;
private int[] tabTitles = new int[] {R.string.head_character, R.string.head_training, R.string.head_evolutions, R.string.head_talismans, R.string.head_enchantments};
public SimpleFragmentPagerAdapter(FragmentManager fm , Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new TrainingFragment();
} else {
return new EvoFragment();
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return context.getResources().getString(tabTitles[position]);
}
}