How to get String Resource within ViewPager Adapter?

那年仲夏 提交于 2019-11-29 12:56:08

问题


I trying to set the title for my viewpager, I can't seem to get it to work. I tried Resources.getSystem().getString(R.string.title1); and also tried to pass a context. Could anyone help me?

public class ViewPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    Context context;

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

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

    @Override
    public Fragment getItem(int position) {
        return PageFragment.create(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {

        case 0:
            return Resources.getSystem().getString(R.string.title1);

        case 1:
            return Resources.getSystem().getString(R.string.title1);

        case 2:
            return Resources.getSystem().getString(R.string.title1);
        }

        return null;
    }
}

Logcat:

09-02 17:40:33.160: E/AndroidRuntime(3116): FATAL EXCEPTION: main
09-02 17:40:33.160: E/AndroidRuntime(3116): android.content.res.Resources$NotFoundException: String resource ID #0x7f050003
09-02 17:40:33.160: E/AndroidRuntime(3116):     at android.content.res.Resources.getText(Resources.java:230)
09-02 17:40:33.160: E/AndroidRuntime(3116):     at android.content.res.Resources.getString(Resources.java:314)

回答1:


Well, according to your error message you do not have a String with the ID you are requesting.

Could it be that you are supporting multi languages and only have this kind of String-ID for a specific language?

Anyways, this line of code:

String yourstring = getResources().getString(R.string.yourstring);

is how to get a String from Resources.




回答2:


you should call getString from an activity's context, change your code to

public class ViewPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    Context context;

    public ViewPagerAdapter(FragmentManager fm, Context nContext) {
        super(fm);
        context = nContext;
    }

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

    @Override
    public Fragment getItem(int position) {
        return PageFragment.create(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {

        case 0:
            return context.getString(R.string.title1);

        case 1:
            return context.getString(R.string.title1);

        case 2:
            return context.getString(R.string.title1);
        }

        return null;
    }
}



回答3:


If you are trying to access string resources from outside of an activity, then you need to pass the context in to that class (which you stated you have already done), and then call

String str = context.getResources().getString(R.string.some_string);

Also check your strings.xml file to make sure that you are using the string id correctly. If you're using android studio you should be able to simply start typing R.string. and it will then show suggestions from the strings.xml file. If you do not see your resource here, then that may indicate a problem with the way you are storing your resources, as opposed to how you are trying to access them.




回答4:


Just use the available using: getContext().getString(resId) like:

getContext().getString(R.string.title1)

It worked for me.




回答5:


You are trying to fetch String inside your Resources without gaining Resources. So try to pull your Resources with getResources() with your Context.

If your are inside Activity or Service then context is there. If outside then try to pull Context first with getApplicationContext() and on that pull the resources and pull your String in the end on it.

As per your Code these snippet would't throw any exception if resources and mentioned String id's are pulled properly.

 public CharSequence getPageTitle(int position) 
  {
    switch (position) 
    {
    case 0:
           return getResources().getString(R.string.title1);
    case 1:
           return getResources().getString(R.string.title1);
    case 2:
           return getResources().getString(R.string.title1);
     }
    return null;
  }



回答6:


In my experience, the view pager won't show each text to the assigned screen consistently. I needed to create separate layout for each page and assign the text from within the layout, so when flipping the pages each page will show a layout with its text.




回答7:


Try :

getApplicationContext().getResources().getString(R.String.title1);



回答8:


If it is not allowed to call getResources() in your viewPager Adapter, you need to declare it as a static variable in the parent activity




回答9:


With Kotlin I do this by sending context to the adapter, as below:

class FmAdapter(val context: Context, fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val fragments = arrayListOf<Fragments>()

    init {
        fragments.add(Fragments(R.string.title1, FragmentOne()))
        fragments.add(Fragments(R.string.title2, FragmentTwo()))
    }

    override fun getCount(): Int = fragments.size

    override fun getItem(position: Int): Fragment = fragments[position].fragment

    override fun getPageTitle(position: Int): CharSequence? = context.getString(fragments[position].title)
}

with data class:

data class Fragments(@StringRes val title: Int, val fragment: Fragment)

and simple call from activity, like:

rv.adapter = FmAdapter(this, supportFragmentManager)


来源:https://stackoverflow.com/questions/18578567/how-to-get-string-resource-within-viewpager-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!