在活动类中,我有这个类(来自android示例):
public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new QuestionFragment();
Bundle args = new Bundle();
args.putInt(QuestionFragment.ARG_OBJECT, i );
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return questionList.length;
}
@Override
public CharSequence getPageTitle(int position) {
return "Title n°" + (position + 1);
}
}
我想改变这个:返回“标题n°”+(位置+ 1); to:返回getActivity()。getResources()。getString(R.string.questionTabTitle)+(position + 1);
但活动未定义。我怎样才能获得我需要的字符串资源?
答案 0 :(得分:18)
您可以修改此类的构造函数,并将父活动的上下文作为参数传递:
private Context _context;
//Constructor of the class
public DemoCollectionPagerAdapter(FragmentManager fm, Context c) {
super(fm);
_context = c;
}
然后在getPageTitle
函数中,您可以使用类中定义的新上下文访问资源:
_context.getResources().getString(R.string.questionTabTitle) + (position + 1);