7

I'm developing a simple app, that contains tabview with fragment. I am stuck at the place, where i have to pass data to my newly created fragment on tabselect.

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

List<List<NewsObjectClass>> myList;

Here is where I got 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;
    }
...

So specifically i need a way how to pass my list of lsits of myCustomObjects to the fragment, so there I could use it for lsitview adapter.

Any syggestions on how to pass this type of data would be great. Thanks.

xenuit
  • 97
  • 1
  • 1
  • 9

4 Answers4

21
args.putParcelableArrayList(DATA_KEY, new ArrayList<>(data));
Alireza Sobhani
  • 769
  • 1
  • 8
  • 18
10

Make your NewObjectClass Parcelable or Serializable and then create a new class, effectively, containing your list, also Parcelable or Serializable. Then use Bundle.putSerializable (or putParcelable)

Or, simpler, make NewObjectClass Parcelable then use putParcelableArrayList if you can do with ArrayList instead of generic List

Or, simplest, make NewObjectClass Serializable and use putSerializable passing ArrayList<NewObjectClass> because ArrayList is Serializable

In the last case perhaps you only will have to ad implements Serializable to your class.

Alternatively, if your data seem to be large, consider keeping them in a custom Application-derived object instead. You extend Application and then such object will exist all the time your app exist. Don't forget to register it in manifest.

class MyApplication extends Application {
   public static Object myData;
}

Or you can do with shared preferences

PreferenceManager.getDefaultSharedPreferences().edit().putInt("a", 1).commit();
PreferenceManager.getDefaultSharedPreferences().getInt("a");
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Is there a way doing this data-pass without serialization or so? Is this the only way? – xenuit May 03 '14 at 07:05
  • If your class is simple enough you perhaps will only need to add implements Serializable then pass ArrayList to Bundle. Jsva takes care of serialization – Alexander Kulyakhtin May 03 '14 at 07:07
  • 3
    @xenuit use Parcelable instead of Serializable, for more info see [this](http://blog.3pillarglobal.com/parcelable-vs-java-serialization-android-app-development) – Shayan Pourvatan May 03 '14 at 07:09
  • Well, my class contains only String and Bitmap objects. And one more thing - isnt it a problem of using List> ? – xenuit May 03 '14 at 07:15
  • Your data seem to be large. Consider keeping them in a custom Application-derived object instead. You extend Application and then such object will exist all the time your app exist. Don't forget to register it in manifest. – Alexander Kulyakhtin May 03 '14 at 07:26
  • Those data are pretty large. Maybe i should consider of using database? Or that isnt a good way? (Have never used it before). And will it be possible to access those database data from everywhere? – xenuit May 03 '14 at 07:57
  • @Alex If you go that route, be prepared for that data to go missing at any time after your application goes into the background. Preferably, store it persistently (e.g. in a SQLite database) and pass an identifier to the new Activity, allowing it to look up the data. – Kevin Coppock May 03 '14 at 07:58
  • @xenuit From what I can see, a database definitely sounds like the proper solution for your data set. – Kevin Coppock May 03 '14 at 07:59
  • If data a really large passing them in an Intent is not an option as it will freeze the application for a long time. Might be you can do with SharedPreferences instead of database they are easier – Alexander Kulyakhtin May 03 '14 at 08:09
  • Well, thats an option, Alex. Seems that today I'll have a tutorial evening. Cus no doubt of how to use SharedPrefs, lol. =) Anyway, thanks for help. – xenuit May 03 '14 at 08:12
5

use putSerializable method to pass your custom list.

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

and fetch it using getSerializable

ArrayList<Type> list = (ArrayList<Type>) getArguments().getSerializable(KEY);
Satendra
  • 6,755
  • 4
  • 26
  • 46
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62
1

Or, simplest, make NewObjectClass Serializable and use putSerializable passing ArrayList because ArrayList is Serializable

Unfortunately, this did not work in my case. I ended up converting my array list to string (or JSON string) and send by

bundle.putString()

and then, in fragment, parsed the string back to array list.

Taha
  • 117
  • 1
  • 11