Saving ArrayList Hash Map

in my application I want to save the data in savedInstanceState() . I want to save ArrayList<HashMap<String,String>> . And while I can’t do it. here is my code that bothers me

 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList("places", (ArrayList<? extends Parcelable>) places); } 

restore () method

 private void restore(Bundle savedInstanceState) { // TODO Auto-generated method stub //What should i do here! i have try many things but none of them is helping } 
+6
source share
2 answers

Since ArrayList , HashMap and String are Serializable , you can use Bundle.putSerializable and Bundle.getSerializable

 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable("places", places); } private void restore(Bundle savedInstanceState) { if (savedInstanceState != null) { places = (ArrayList<HashMap<String,String>>) savedInstanceState.getSerializable("places"); } } 

Also, make sure you call restore from onRestoreInstanceState or onCreate

+24
source

say that:

  ArrayList< HashMap< String,String>> arr = new ArrayList< HashMap< String,String>> (); @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList("places", (ArrayList< ? extends Parcelable>) arr); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { ArrayList<HashMap<String,String>> a = (ArrayList<HashMap<String,String>>)savedInstanceState.get("places"); /*for(int i =0; i< a.size(); i++){ HashMap<String,String> map = a.get(i); for(String s : map.keySet()){ Log.d("log",s+" :: "+map.get(s)); } }*/ } 
0
source

All Articles