如何在Android中将Fragment传递给MainActivity?

时间:2014-01-27 11:02:41

标签: android android-fragments tabs

由于我是android的新手,我在使用片段实现 ActionBarTab 和swipable viewpager时坚持了一些观点

我有3个标签,每个标签都有一些控件,最后一个标签会将所有标签数据提交到一个表格 我浏览下一个按钮并使用标签更改事件

使用下一个标签我可以使用getter / Setter将数据设置为Class对象,然后我将该类数据提交到DB表进行保存..

现在我想在标签更改事件时调用相同的方法 方法是 SetdataToModelClass()

如何在按钮点击事件上调用的标签更改事件上调用相同的方法,这样如果用户浏览标签而不是下一个按钮并更改任何数据,则调用My SetdataToModelClass()

SetdataToModelClass位于每个片段中,其中我的Tab更改事件在MainFragmentActivity类中,因此如何在制表符更改事件上调用SetdataToModelClass()方法(即希望在MainFragment Act& Fragment之间进行通信)

MainActivity Class的代码在这里:

public class TestFragmentTabHost extends FragmentActivity implements TabListener {
ViewPager vp;
ActionBar ab;   
SalesActivity sa = new SalesActivity();
SessionManager session = null;
String usrNm = null;    
String szImeiId = null;
Spinner spnAECust;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabhost_act);

    /* Action Bar Color change on create*/  
    ActionBar actionBar = getActionBar();
    ActionBarColor.setBackgroundColor(actionBar);
    getActionBar().setDisplayHomeAsUpEnabled(true);     
    getActionBar().setTitle(Html.fromHtml("<font color=\"white\" face=\"verdana,arial\">" + getString(R.string.air) + "</font>"));

    // Session Manager
    session = new SessionManager(getApplicationContext());

    /* To Get Unique Device id */
    TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    szImeiId = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE.

    // get User Details from Session
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap = session.getUserDetails();
    usrNm = hashMap.get(SessionManager.KEY_USRNM);      

    vp = (ViewPager) findViewById(R.id.pager);
    vp.setAdapter(new MyPageAdapter(getSupportFragmentManager()));

    ab = getActionBar();
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = ab.newTab();
    tab1.setText("INFO");
    tab1.setTabListener(this);

    ActionBar.Tab tab2 = ab.newTab();
    tab2.setText("PORT");
    tab2.setTabListener(this);


    ActionBar.Tab tab3 = ab.newTab();
    tab3.setText("PACKAGES");
    tab3.setTabListener(this);

    ab.addTab(tab1);
    ab.addTab(tab2);
    ab.addTab(tab3);        

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    vp.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
}

// For Page adapter
class MyPageAdapter extends FragmentPagerAdapter {

    public MyPageAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment frgmnt = null;
        if (arg0 == 0) {
            frgmnt = new InfoFragment();                    
        }else if (arg0 == 1) {
            frgmnt = new PortFragment();
        }else if (arg0 == 2) {
            frgmnt = new PackagesFragment();
        }
        return frgmnt;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}




 public void setSalesInfoData(List<String> sales) {     
        while (sales.size() > 0) {                  
            sa.setCustomerId(Integer.parseInt(sales.get(0).toString()));
            sa.setProspectId(Integer.parseInt(sales.get(1).toString()));
            sa.setCommodityId(Integer.parseInt(sales.get(2).toString()));
            sa.setSpecialNotes(sales.get(3));
            sa.setLob(sales.get(4));
            DateFormat dt = new DateFormat();
            Date crTs = dt;
            sa.setCrTs(crTs);
            sa.setCrUsr(usrNm);         
            sa.setDeviceId(szImeiId);
            break;
        }

    }

使用方法将setdata设置为Class SalesActivity

1 个答案:

答案 0 :(得分:2)

将适配器更改为:

class MyPageAdapter extends FragmentPagerAdapter {
    Fragment[] fragments=new Fragment[3];

    public MyPageAdapter(FragmentManager fm) {
        super(fm);
         fragments[0]= new InfoFragment();                    
         fragments[1]= new PortFragment();
         fragments[2]= new PackagesFragment();
    }

    @Override
    public Fragment getItem(int arg0) {
       return fragments[arg0];
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }
}

onTabUnselected中,片段调用函数ex:

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
     Fragment fragment=adapter.getItem(tab.getPosition());
   if(fragment instanceof InfoFragment){
       ((InfoFragment)fragment).SetdataToModelClass();
   }
   if(fragment instanceof PortFragment){
       ((PortFragment)fragment).SetdataToModelClass();
   }
   if(fragment instanceof PackagesFragment){
       ((PackagesFragment)fragment).SetdataToModelClass();
   }
}

同时使适配器对象成为类变量ex:

ontside onCreate

MyPageAdapter adapter;
onCreate

中的

adapter=new MyPageAdapter(getSupportFragmentManager());
vp.setAdapter(adapter);