Passing a custom data list using the bundle

I am developing a simple application that contains a tabview with a snippet. I am stuck in the place where I need to transfer data to my newly created tabselect fragment.

I have a list of lists of my custom class objects:

List<List<NewsObjectClass>> myList;

This is where I am stuck:

public static class PlaceholderFragment extends ListFragment{

    private static final String ARG_SECTION_NUMBER = "section_number";


    public PlaceholderFragment(){       

    }


    public static PlaceholderFragment newInstance(int sectionNumber, List<List<NewsObjectsClass>> data)  {

        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);

        // Here i want to pass my List<List<NewsObjectClass>> to the bundle

        fragment.setArguments(args);
        return fragment;
    }
...

Therefore, I especially need a way to pass my lsits myCustomObjects list to a fragment, so I could use it for the lsitview adapter.

Any syggestions on how to pass this data type would be great. Thank you

+4
source share
3 answers

NewObjectClass Parcelable Serializable, , , Parcelable Serializable. Bundle.putSerializable ( putParcelable)

, NewObjectClass Parcelable, putParcelableArrayList, ArrayList List

, , NewObjectClass Serializable putSerializable, ArrayList<NewObjectClass>, ArrayList Serializable

implements Serializable .

, , , Application - . Application, . .

class MyApplication extends Application {
   public static Object myData;
}

PreferenceManager.getDefaultSharedPreferences().edit().putInt("a", 1).commit();
PreferenceManager.getDefaultSharedPreferences().getInt("a");
+8
args.putParcelableArrayList(DATA_KEY, new ArrayList<>(data));
+15

use the putSerializablemethod to pass a custom list.

args.putSerializable(KEY, ArrayList<Type>);

and remove it with getSerializable

ArrayList<Type> list = (ArrayList<Type>) getArguments().getSerializable(KEY);
+1
source

Source: https://habr.com/ru/post/1539091/


All Articles