How to get String Resource within ViewPager Adapter?

后端 未结 9 1829
孤城傲影
孤城傲影 2020-12-31 07:42

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. Coul

相关标签:
9条回答
  • 2020-12-31 08:23

    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.

    0 讨论(0)
  • 2020-12-31 08:29

    Try :

    getApplicationContext().getResources().getString(R.String.title1);
    
    0 讨论(0)
  • 2020-12-31 08:30

    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. getResources() can be called whenever you are in a class extending Context, such as Activity.

    0 讨论(0)
  • 2020-12-31 08:30

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

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

    It worked for me.

    0 讨论(0)
  • 2020-12-31 08:32

    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)
    
    0 讨论(0)
  • 2020-12-31 08:40

    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;
      }
    
    0 讨论(0)
提交回复
热议问题