0

I am having problems using the FragmentPagerAdapter. I want to retrieve the data from fragments in a View Pager. When I call the getItem() Method it returns a Fragment, but it doesn’t create the fragment. All the fragments variables are null. What do I need to do to instantiate the fragment? This is my adapter.

public class SubItemAdapter extends FragmentPagerAdapter implements Serializable {
    List<Item> itemList = new ArrayList<>();
    int layout;


    public SubItemAdapter(FragmentManager mgr, List<? extends Item> items, int layout) {
        super(mgr); 
        itemList.addAll(items);
        this.layout = layout; 

    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public SubItemFragment getItem(int position) {
        return SubItemFragment.newInstance(itemList.get(position), layout); 
    }
}

This is the fragment in the viewpager.

public class SubItemFragment extends Fragment {
Item item;
View view;
Context mContext;
private static final int RC_BARCODE_CAPTURE = 9001;
private static final String ITEM_KEY = "item";
private static final String LAYOUT_PAGER_KEY = "layout";

public static SubItemFragment newInstance(Item item, int layout) {
    SubItemFragment frag = new SubItemFragment();
    Bundle args = new Bundle();
    args.putSerializable(ITEM_KEY, item);
    args.putInt(LAYOUT_PAGER_KEY, layout);
    frag.setArguments(args);
    return (frag);
}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {

    int layout = getArguments().getInt(LAYOUT_PAGER_KEY);
    view = inflater.inflate(layout, container, false);
    item = (Item) getArguments().getSerializable(ITEM_KEY); 
    mContext = getContext();
    /* Populate the fragment with data
    . . .
    */
    return (view);
    }

    public Item getItem() {
        item.retrieve_data_and_populate_item();
        return item;
    }
}

I don’t have a great understanding of how the ViewPager works but from what I understand the reason the fragment is coming back without any variables set is because the ViewPager isn’t attaching the fragments. How do I get the getItem(int position) function to return an initialized fragment? If my question isn’t clear enough or if you need to see more code, just let me know. Thanks in advance.

Edit

I am setting the adapter in the parent fragment. Here is the code for adding the ViewPager:

public class General_Fragment implements SubItemFragment.subItemListener {

    private static final int LAYOUT_PAGER_KEY = R.layout.sub_form_meter_read;
    private SubItemAdapter mAdapter;
    private WrapViewPager mPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.workorder_detail, container, false);
    meters = getMeterList();
    mPager = rootView.findViewById(R.id.meter_list);
    mPager.addOnPageChangeListener(getOnPageListener());
    mAdapter = buildAdapter(meters);
    mPager.setAdapter(mAdapter);
    return rootView;
    }

    /*. . .*/ 

    private SubItemAdapter buildAdapter(List<? extends Item> items) {
        return new SubItemAdapter(getChildFragmentManager(), //Fragment View
                items, //Item to get data from
                LAYOUT_PAGER_KEY
        );
    }

}

I don't think my problem lies in initializing the adapter because It does display but when I go to retrieve data from the fragment that's where it crashes because of the empty fragment

nickc
  • 1,193
  • 1
  • 6
  • 15

1 Answers1

1

I found a similar question that explained what I was looking at was wrong. This is the answer that lead me to my solution.

https://stackoverflow.com/a/14039416/9301369

Basically as he says getItem() doesn’t retrieve the already made fragments like I thought. It’s in fact used to create new fragments. This is what was causing me to get fragments that wouldn’t come back null but wouldn’t be instantiated either. So what I did is I added the fragment manager private FragmentManager mgr; to the adapter varaibles and set it in the constructor

public SubItemAdapter(FragmentManager mgr, List<? extends Item> items, int layout) {
    super(mgr); 
    this.mgr = mgr;
    itemList.addAll(items);
    this.layout = layout; 
}

Then searched the fragment manger to get the specific fragment I needed. Passing in the position and pager id, mPager.getID.

public SubItemFragment getSubItemFragment(int viewPagerId, int fragmentPosition) {
    return (SubItemFragment) mgr.findFragmentByTag("android:switcher:" + viewPagerId + ":" + fragmentPosition);
}

So basically, my assumption that the ViewPager wasn’t attaching the fragments was wrong. The fragments were attached I was just referencing newly created fragments.

nickc
  • 1,193
  • 1
  • 6
  • 15