问题描述
- 保存哈希图的ArrayList
-
在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 }
解决方案
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));
}
}*/
}
解决方案二:
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");
}
}
还有注意从onRestoreInstanceState
或者onCreate
调用restore
时间: 2025-01-15 23:40:29