在我的应用中,我想在savedInstanceState()
保存数据。我想保存ArrayList<HashMap<String,String>>
。到目前为止,我无法做到这一点。
这是我困扰我的代码
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("places", (ArrayList<? extends Parcelable>) places);
}
restore()方法
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
}
答案 0 :(得分:25)
由于ArrayList
,HashMap
和String
为Serializable
,您可以使用Bundle.putSerializable
和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");
}
}
此外,请务必从restore
或onRestoreInstanceState
onCreate
答案 1 :(得分:0)
假设你有:
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));
}
}*/
}