2 for 2012 for_2012 于 2013.02.18 15:01 提问

保存哈希图的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

    }

2个回答

vikeyToy
vikeyToy   2013.02.18 16:48
  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));
        }
    }*/
}
g989_1314125
g989_1314125   2013.02.18 17:00

ArrayList, HashMap和String是Serializable,你可以使用Bundle.putSerializableBundle.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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!