gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:58:18 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com