1

嗨!我在我的 Android 应用程序中使用带有 Fragmenst 的 ViewPager。片段正在工作,但会丢失显示的数据。所以在第一次调用我的视图适配器时,我可以将数据从片段 1 和 2 设置到线性布局,片段 3 不可用。所以我将数据放在片段 1 和 2 上。当我现在将片段从 1 切换到 3 时,第一个片段会丢失数据并变为空白。如何修复数据以显示它一直显示?如果要动态创建信息而不是在 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);
    }
}

}

4

0 回答 0