Search code examples
javaandroidandroid-fragmentsandroid-viewpager

How can I use a ViewPager to display the same fragment but loaded with different data each time?


I'm trying to set up a ViewPager to display the same template fragment but populated with different data after each swipe. I'm using dummy data here, but my problem is that the same data is being loaded after each swipe. What exactly am I doing wrong?

Here is my code:

Test:

public class Test {
    private String a;
    private String b;
    private int c;

    public Test(String a, String b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public static final Test[] testData = {
            new Test("Title 1", "Description 1", 1),
            new Test("Title 2", "Description 2", 2),
            new Test("Title 3", "Description 3", 3),
            new Test("Title 4", "Description 4", 4),
            new Test("Title 5", "Description 5", 5)
    };

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public int getC() {
        return c;
    }
}

TestFragment:

public class TestFragment extends Fragment {

    private static final String ARG_ID = "Id";
    private int Id;
    private String a;
    private TextView aTV;
    private String b;
    private TextView bTV;
    private int c;
    private TextView cTV;

    public static TestFragment newInstance(int Id) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_ID, Id);

        TestFragment fragment = new TestFragment();
        fragment.setArguments(args);
        return fragment;
    }

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

        Id = (Integer) getActivity().getIntent().getExtras().get(EXTRA_ID);
        a = Test.testData[Id].getA();
        b = Test.testData[Id].getB();
        c = Test.testData[Id].getC();

        aTV = (TextView) view.findViewById(R.id.a);
        aTV.setText(a);

        bTV = (TextView) view.findViewById(R.id.b);
        bTV.setText(b);

        cTV = (TextView) view.findViewById(R.id.c);
        cTV.setText(String.valueOf(c));

        return view;
    }
}

ViewPagerActivity:

public class ViewPagerActivity extends AppCompatActivity {

    private ViewPager viewPager;

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

        viewPager = (ViewPager) findViewById(R.id.view_pager);

        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return TestFragment.newInstance(position);
            }

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

Solution

  • You are not getting the fragment arguments of the TestFragment in which You have the Id variable but instead You are trying to get the data from the Intent Extras:

    Id = (Integer) getActivity().getIntent().getExtras().get(EXTRA_ID);
    

    You should get the arguments that You have provided in the newInstance method of TestFragment this way:

    Id = getArguments().getInt(ARG_ID);