【问题标题】:how to use ArrayString for ViewPager's TextView.setText in android?如何在 android 中为 ViewPager 的 TextView.setText 使用 ArrayString?
【发布时间】:2020-01-10 11:54:51
【问题描述】:

我想在我的 Android 应用中使用 TabActivity。每个 viewPager 的页面包含 2 个 TextView 和 1 个 ImageView。我想用ArrayString而不是简单的String写下我的文本,但我不知道如何在TextView.setText中调用它?

我的片段类:

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = " " ;
    public TextView label_TV, info_TV;
    public  ImageView photo_IMG;


    public PlaceholderFragment() {
    }


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_bio, container, false);

        label_TV = (TextView) rootView.findViewById(R.id.section_label);

        //label_TV.setText(?);

        info_TV = (TextView) rootView.findViewById(R.id.section_info);
        photo_IMG = (ImageView) rootView.findViewById(R.id.section_image);
        return rootView;
    }
}

FragmentPagerAdapter 类:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:

                return "SECTION 1";

           ..
                          }
        return null;
    }
}

字符串文件:

<string-array name="titles">
    <item>00</item>
    <item>11</item>
</string-array>

我如何将titles arrayString 用于label_TV

【问题讨论】:

    标签: android android-fragments textview android-viewpager tabactivity


    【解决方案1】:

    也许,最好的方法是通过 newInstance() 方法。如果它当然不是动态的:)

    【讨论】:

      【解决方案2】:

      更换你的线路

      return PlaceholderFragment.newInstance(position + 1);
      

      return PlaceholderFragment.newInstance((position + 1),your_titles_array,fragment_position);
      

      现在用

      替换你的 newInstance()
      public static PlaceholderFragment newInstance(int sectionNumber,String[] titlesArray, int fragment_position) {
          PlaceholderFragment fragment = new PlaceholderFragment();
          Bundle args = new Bundle();
          args.putInt(ARG_SECTION_NUMBER, sectionNumber);
          args.putStringArray("key_title_array",temp);
          args.putInt("key_fragment_position", fragment_position);
          fragment.setArguments(args);
          return fragment;
      }
      

      在 Oncreate 视图中

          Bundle bundle=getArguments(); 
          String[] titlesArray=bundle.getStringArray("key_title_array");
          int fragment_position=bundle.getInt("key_fragment_position");
      
          label_TV.setText(titlesArray[fragment_position]);
      

      希望这项工作:)

      【讨论】:

      • 在哪里识别 ArrayString? key_title_array 是什么?
      • ArrayString from string file "titles" 像 getResources().getStringArray(R.array.titles) 和 "key_title_array" 和你的 "ARG_SECTION_NUMBER" 一样
      猜你喜欢
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2012-03-18
      • 2011-06-21
      相关资源
      最近更新 更多