how to use ArrayString for ViewPager's TextView.setText in android?

39 Views Asked by At

I want to use TabActivity in my android app. each viewPager's pages contains 2 TextView and 1 ImageView. I want to write down my texts in ArrayString instead of simple String,but i don't know how to call it inTextView.setText?

my Fragment class:

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 class:

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 file:

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

how can i use titles arrayString for label_TV?

2

There are 2 best solutions below

0
Mike On

Probably, the best place to do so is via newInstance() method. If it is not dynamic of course :)

2
pratik vekariya On

Replace your line

return PlaceholderFragment.newInstance(position + 1);

with

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

Now replace your newInstance() with

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;
}

In Oncreate view

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

    label_TV.setText(titlesArray[fragment_position]);

hope this work :)