java - 文字不显示(初学者)

标签 java android android-fragments

我是 Java 初学者,学习了一些东西并决定尝试一下。到目前为止,我已经用 C++ 编写了一年多的代码,并且到目前为止我已经注意到 Java 编码中的一些相似之处。我习惯于通过传递字符串的名称和我想要显示的元素(即: string[i]="a")来简单地传递字符串的元素,这非常简单,但是在 java 中,我遇到过将字符串及其位置传递给某些 fragment (使用 View 分页器)时出现一些问题。 简而言之,我似乎无法在 fragment 上显示任何文本。我知道我的编码很困惑,但我真的很感激任何帮助。

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

最佳答案

替换

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

dummyTextView.setText(getmText());

或者

dummyTextView.setText(getArguments().getString("先生"));

这应该有效。

关于java - 文字不显示(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33487499/

相关文章:

android - 了解 fragment 堆栈

java - 如何通过 Autowired 注入(inject)具有不同配置的不同 ObjectMappers?

java - 如何在 Java 中格式化和打印多个字符串

java - Android try/catch 不起作用

android - 当子弹击中某物时,如何在 Unity 中摇晃相机?

android - 如何将Onclick监听器设置为 fragment 中的按钮

java - 将未指定范围的数字存储到数组中

java - android AccessibilityService 突然停止触发事件 - 请检查我的代码

android - 如何在编译 APK(Release) 时排除文件夹

java - 带有 recyclerview 和 tablayout 的动态高度 viewpager