问题描述
在扩展android.support.v4.app.FragmentPagerAdapter类,有没有什么办法让访问Context.getString(..)方法,而不扩展的类作为一个内部类的活动,或者传递一些背景从外部?
In a class extending android.support.v4.app.FragmentPagerAdapter, is there any way to get access to the Context.getString(..) method without the extending class being an inner class of an activity or passing in some context from the outside?
感谢您的任何暗示!
推荐答案
我有一个类似的问题。从一个抽屉布局,我想在决定一个从助手类调用的方法要使用的片段。
I had a similar issue. From a drawer layout, I wanted to decide which fragment to use in a method called from a helper class.
因此,在onCreateView ...
So in onCreateView...
String form = getResources().getStringArray(R.array.drawer_array)[i];
Context context = getActivity().getApplicationContext();
FragmentHelper fh = new FragmentHelper();
int myFragment = fh.getCurrentFragment(form,context);
和公共FragmentHelper()...
And in public FragmentHelper()...
public int getCurrentFragment(String form, Context context){
...
context.getResources().getString(R.string.label_item1);
...
}
诀窍是在getResources前面添加上下文()。否则,我的堆栈表明片段不附加到的活性。
The trick being to add context in front of getResources(). Otherwise, my stack showed that the fragment was not attached to an activity.
希望这可以帮助别人。
这篇关于访问的getString()在android.support.v4.app.FragmentPagerAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!