将参数从Activity传递到另一个Activity Fragements

时间:2016-06-02 12:04:12

标签: java android android-fragments

我的主要活动包含我需要传递给所有应用程序活动的主要参数。 (用户名 - 用户权限 - 团队...... ETC)。

我知道怎么做,除了包含三个片段的活动,使用pager和tablayout显示。如何将参数传递给所有这些片段? ..我应该将它传递给活动然后将其传递给三个片段作为第二步吗?如果是,我如何传递给下面代码中的片段:

Second Activity Java:

public class DCODatabase extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dcodatabase);

    Toolbar toolbar = (Toolbar) findViewById(R.id.DCODatabaseToolbar);
    setSupportActionBar(toolbar);
    assert toolbar != null;
    toolbar.setLogo(R.drawable.dco1);

    //Receving bundle from main activity// --> Working
    Bundle extras = getIntent().getExtras();

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab);
    assert tabLayout != null;
    tabLayout.addTab(tabLayout.newTab().setText("General Reports"));
    tabLayout.addTab(tabLayout.newTab().setText("Report Display"));
    tabLayout.addTab(tabLayout.newTab().setText("New Report"));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount(),extras);
    assert viewPager != null;
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}
}

寻呼机适配器:

public class PagerAdapter extends FragmentStatePagerAdapter{
int mNumOfTabs;
private final Bundle fragmentBundle;
public PagerAdapter(FragmentManager fm, int mNumOfTabs,  Bundle bundle) {
    super(fm);
    this.mNumOfTabs = mNumOfTabs;
     this.fragmentBundle = bundle; ---> Receving in Adapter.
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            DCOGeneralReports tab1 = new DCOGeneralReports();
            tab1.setArguments(fragmentBundle); --> Sending to fragement
            return tab1;
        case 1:
            DCOReportDisplay tab2 = new DCOReportDisplay();
            return tab2;
        case 2:
            DCO_New_Report tab3 = new DCO_New_Report();
            return tab3;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

片段示例:

public class DCOGeneralReports extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dco_general_report, container, false);

// here the problem. (SavedInstance is null! --> Not Receving the data)
Name = savedInstanceState.getString("Name");
        UserGroup = savedInstanceState.getString("UserGroup");
        Team = savedInstanceState.getString("Team");

1 个答案:

答案 0 :(得分:1)

在创建适配器实例时,您可以传递参数的内部活动类,以便您可以在适配器的构造函数中检索该值。

PagerAdapter的getItem()内部方法试试这个

   Bundle =new Bundle();
    b.putString("key","value");
    tab1.setArgument(b);
    return tab1;

并且在最终活动中你需要为每个活动传递参数。你可以使用getIntent.putExtra()来编写它,以便以后可以使用getExtra("key") <来检索它。 / p>

为了传递值,请创建一个像这样的实例

    ArrayList demo=new ArrayList();
    demo.add("A");
    final PagerAdapter adapter = new PagerAdapter
        (getSupportFragmentManager(), tabLayout.getTabCount(),demo);

在适配器类中更改构造函数

ArrayList frag;

public PagerAdapter(FragmentManager fm, int mNumOfTabs,ArrayList frag) {
    super(fm);
    this.mNumOfTabs = mNumOfTabs;
    this.frag = frag;
}