如何将LinkedHashMap从FragmentPagerAdapter传递给Fragment?

时间:2016-01-05 18:51:58

标签: java android android-fragments

下面是使用args.putInt.传递Int值的示例。同样,我想传递employee_map。如何将Map对象作为Bundle args传递给Fragments。我不想以列表的形式传递。

MyFragmentPageAdapter扩展了FragmentPagerAdapter

@Override
   public Fragment getItem(int position) {
      LinkedHashMap<String,Employee> employee_map;
      Fragment fragment = new DummySectionFragment();
      Bundle args = new Bundle();
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
      fragment.setArguments(args);
      return fragment;
    }

MyFragment扩展片段

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       int fragmentNumber = getArguments().getInt(ARG_SECTION_NUMBER);
}

1 个答案:

答案 0 :(得分:0)

MyFragmentPageAdapter

 @Override
       public Fragment getItem(int position) {
          LinkedHashMap<String,Employee> employee_map;
          Fragment fragment = new DummySectionFragment();
          Bundle args = new Bundle();
          args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
          args.putSerializable("employee_map", employee_map);
          fragment.setArguments(args);
          return fragment;
        }

MyFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       int fragmentNumber = getArguments().getInt(ARG_SECTION_NUMBER);
       employee_map = (LinkedHashMap<String, Employee>) getArguments().getSerializable("employee_map");
}
  

重要提示:Employee实施java.io.Serializable