Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving ArrayList of Hash map

Tags:

android

in my app i want to save data at savedInstanceState(). i want to save ArrayList<HashMap<String,String>>. And so far i am unable to do that. here is my code that is bothering 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

    }
like image 541
Usman Riaz Avatar asked Feb 17 '13 17:02

Usman Riaz


1 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 are calling restore from onRestoreInstanceState or onCreate

like image 192
Vladimir Mironov Avatar answered Oct 19 '22 05:10

Vladimir Mironov