Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashmap as param for a Fragment

I have a fragment with an ExpandableList which content is provided by a HashMap<String,List<String>>. This HashMap<String,List<String>> is filled in Fragment's Activity and i need to pass it to the fragment.

To do it, i was planning to do a Fragment transaction but i found the problem:

In the method "newInstance" of the fragment, when setting arguments to the Bundle, there is no method to put a HashMap.

Ex:

Bundle args = new Bundle();
args.putInt(ARG_PARAM1,index); //method .putHashmap doesn't exist.

So, how can i pass my hashmap in bundle to the Fragment? Please provide an solution for this one.Thanks in advance

like image 708
SamuelPS Avatar asked Nov 28 '22 06:11

SamuelPS


1 Answers

To pass HashMap do this

MyFragment fr = new MyFragment(); // Replace with your Fragment class
Bundle bundle = new Bundle();
bundle.putSerializable("hashmap",mMap);
fr.setArguments(bundle);

To retrieve do this

HashMap<String,List<String>> mMap = new HashMap<String,List<String>>();
Bundle b = this.getArguments();
if(b.getSerializable("hashmap") != null)
   mMap = (HashMap<String,List<String>>)b.getSerializable("hashmap");
like image 50
Rohit5k2 Avatar answered Dec 04 '22 12:12

Rohit5k2