HIHO! 我在我的Android应用程序中使用了一个最脆弱的ViewPager。片段正在运行,但丢失了显示的数据。因此,在第一次调用我的View Adapter时,我可以从片段1和2将数据设置为LinearLayout,片段3不可用。所以我将数据放在片段1和2上。当我现在将片段从一个切换到三个时,第一个片段丢失数据并变为空白。如何修复数据以显示它始终显示?如果动态创建信息而不是在xml文件中。 我希望你能理解我并能帮助我。
public class PageAdapter extends FragmentStatePagerAdapter {
private int num_pages;
private String[] titles;
public PageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int pos) {
PageFragment f = new PageFragment(pos);
return f;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return num_pages;
}
@Override
public CharSequence getPageTitle(int position){
return titles[position];
}
public void set_num_pages(int num){
num_pages=num;
}
public void set_titles(String[] a_titles){
titles=a_titles;
}
}
下一课:
public class PageFragment extends Fragment {
private int fragmentNR;
public PageFragment(){}
public PageFragment(int nr) {
this.fragmentNR = nr;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = new View(getActivity());
if (fragmentNR == 0)
v = inflater.inflate(R.layout.info_1, container, false);
else if (fragmentNR == 1)
v = inflater.inflate(R.layout.info_2, container, false);
else if (fragmentNR == 2)
v = inflater.inflate(R.layout.info_3, container, false);
return v;
}
}
下一课:
public class MannschaftInfos extends FragmentActivity {
private String mannschaftInfos;
private int mannschaftID;
private int anzahl;
private String[] titel = new String[3];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the callback listener, to receive a message when an ad was loaded
mannschaftID = getIntent().getExtras().getInt("id");
titel[0]=(String) this.getText(R.string.Info);
titel[1]=(String) this.getText(R.string.aufstellung);
titel[2]=(String) this.getText(R.string.miStatistik);
anzahl=3;
new AbrufMannschaftInfo(this).laden(mannschaftID);
AbrufMannschaftInfo.setMannschaftListesListener(new Datenlistener());
}
private class Datenlistener implements MannschaftInfosListener {
public void mannschaftInfosListenerrueck(String rueck) {
setContentView(R.layout.info_mannschaft);
PageAdapter mPageAdapter = new PageAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
mPageAdapter.set_num_pages(anzahl);
mPageAdapter.set_titles(titel);
mViewPager.setAdapter(mPageAdapter);
mannschaftInfos=rueck;
LinearLayout llf1 = (LinearLayout) findViewById(R.id.ll_fragment1);
llf1.removeAllViews();
llf1.setPadding(5, 5, 5, 5);
llf1.setOrientation(LinearLayout.VERTICAL);
LinearLayout llf2 = (LinearLayout) findViewById(R.id.ll_fragment2);
llf2.removeAllViews();
llf2.setPadding(5, 5, 5, 5);
llf2.setOrientation(LinearLayout.VERTICAL);
LinearLayout llf3 = (LinearLayout) findViewById(R.id.ll_fragment3);
llf3.removeAllViews();
llf3.setPadding(5, 5, 5, 5);
llf3.setOrientation(LinearLayout.VERTICAL);
View ruler_1 = new View(MannschaftInfos.this); ruler_1.setBackgroundColor(Color.parseColor("#FFFFFF"));
View ruler_2 = new View(MannschaftInfos.this); ruler_2.setBackgroundColor(Color.parseColor("#FFFFFF"));
View ruler_3 = new View(MannschaftInfos.this); ruler_3.setBackgroundColor(Color.parseColor("#FFFFFF"));
View ruler_4 = new View(MannschaftInfos.this); ruler_4.setBackgroundColor(Color.parseColor("#FFFFFF"));
MannschaftInfosAufbereiten mInfos = new MannschaftInfosAufbereiten(mannschaftInfos);
mInfos.show_wappen(llf1, MannschaftInfos.this);
llf1.addView(ruler_1,new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 3));
mInfos.show_stadion(llf1, MannschaftInfos.this);
llf1.addView(ruler_2,new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 3));
mInfos.show_ligen(llf1, MannschaftInfos.this);
mInfos.show_stat(llf2, MannschaftInfos.this);
}
}
}