文字没有显示(初学者)

时间:2015-11-02 21:41:51

标签: java android android-fragments

我是Java的初学者,学到了一些东西并决定尝试一下。我到目前为止用C ++编写了一年多的时间,我注意到到目前为止java编码有一些相似之处。我习惯于通过传递字符串的名称和我想要显示的元素(即:string [i] =“a”)简单地处理字符串的元素,非常简单,但在java中,我遇到过将字符串及其位置传递给某些片段(使用viewpager)的一些问题。 简单地说,我似乎无法在我的片段上显示任何文字。我知道我的编码很乱,但我真的很感激任何帮助。

public class MainActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String [] sir = new String[500];
        for(int i = 0; i < 500; i++) {
            sir[i] = "This is Fragment " + i;
        }

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), sir);

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            }

        });
    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        private String [] sir;

        public SectionsPagerAdapter(FragmentManager fm, String [] stringstodisplay) {
            super(fm);
            this.sir = stringstodisplay;
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }


        @Override
        public int getCount() {
            // Show 3 total pages.
            return 500;
        }


    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private String mText; // display this text in your fragment

        public static Fragment getInstance(String text) {
            Fragment f = new Fragment();
            Bundle args = new Bundle();
            args.putString("sir", text);
            f.setArguments(args);
            return f;
        }

        public void onCreate(Bundle state) {
            super.onCreate(state);
            setmText(getArguments().getString("sir"));
            // rest of your code
        }

        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.dummyfragment, container, false);
            TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
            dummyTextView.setText(getArguments().getString(mText));
            return rootView;
        }

        public String getmText() {
            return mText;
        }

        public void setmText(String mText) {
            this.mText = mText;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

替换

dummyTextView.setText(getArguments().getString(mText));

dummyTextView.setText(getmText());

要么

dummyTextView.setText(getArguments().getString("sir"));

这应该有用。