0

I have a FragmentStatePager(or a ViewPager) and it contains on every page a ListView.The user should be able to add values dynamically into it.

So my question is :What is the best way to store these entries?

nick04
  • 7
  • 5

1 Answers1

0

You can store the data from the ListView in an ArrayList which you can keep track of in the PagerAdapter.

public class MyPagerAdapter extends PagerAdapter{
   private ArrayList<T>[] mData;

   @Override
   public int getCount() {
       return mData.length;
   }

   @Override
   public Object instantiateItem(ViewGroup container, int position) {
        ArrayList<T> listViewData = mData[position];
        MyFragment fragment = new MyFragment();
        fragment.setData(listViewData);
        ...
   }
}
Alex Yau
  • 101
  • 3
  • I had an array adapter but it doesn't stored the entries. Do this example store the data really? – nick04 Feb 13 '15 at 12:54
  • Each ListView should have its own ArrayAdapter. The data for that will be set when we call fragment.setData(listViewData). When the data is modified, so will mDad stored in PagerAdapter since Java is pass by reference. – Alex Yau Feb 13 '15 at 16:20