How to send a list of objects by Bundle?

Asked

Viewed 33 times

0

I am working with a tab system, one for each month, and a birthday listing on the respective tabs.

My Adapter is as follows:

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(this)
                .add(R.string.mes1, MesFragment.class, bundles.get(1))
                .add(R.string.mes2, MesFragment.class, bundles.get(2))
                .add(R.string.mes3, MesFragment.class, bundles.get(3))
                .add(R.string.mes4, MesFragment.class, bundles.get(4))
                .add(R.string.mes5, MesFragment.class, bundles.get(5))
                .add(R.string.mes6, MesFragment.class, bundles.get(6))
                .add(R.string.mes7, MesFragment.class, bundles.get(7))
                .add(R.string.mes8, MesFragment.class, bundles.get(8))
                .add(R.string.mes9, MesFragment.class, bundles.get(9))
                .add(R.string.mes10, MesFragment.class, bundles.get(10))
                .add(R.string.mes11, MesFragment.class, bundles.get(11))
                .add(R.string.mes12, MesFragment.class, bundles.get(12))
                .create());

The fragment of the month will receive by bundle the complete list of birthdays and the month itself to be able to perform the filters before the display.

I’ve set up a list of bundles as follows:

ArrayList<Aniversario> listaAniversariantes = new ArrayList<>()
/* a lista foi declarada assim e por hora já está alimentada estaticamente*/

List<Bundle> bundles = new ArrayList<>(12);
        for (int i = 1; i <= 12; i++) {
            Bundle b = new Bundle();
            b.putInt("mes", i);
            b.putSerializable("listaAniversario", listaAniversariantes);
            bundles.add(b);
        } 

In the fragment of the month I am recovering the information from bundle as follows:

int mes = getArguments().getInt("mes");
ArrayList<Aniversario> listaAniversariantes = (ArrayList<Aniversario>) getArguments().getSerializable("listaAniversario");

The month is being recovered correctly, but the list is not. In tests with separate information, when I go through bundle only the month and I create the list within the fragment, everything works normal, when I include the list in the bundle, the break application.

Does anyone know what might be going on?

PS: I’ve considered using Intent, but the function .add("nome da aba", fragmento) allows the inclusion of only one bundle.

1 answer

0

In today’s episode we learned that we should always review our code!

On the Adapter I passed .add(R.string.mes1, MesFragment.class, bundles.get(1)) from 1 to 12 having in mind the months, however the field is of the index of the list, that goes from 0 to 11, the application was breaking because it was trying to access the position 12 inexistent of the list of bundle.

Topic closed and thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.