2014-10-26 150 views
-1

Arraylist状态带来另一个问题抱歉。无法保存片段

我尝试过自己解决,但是我做不到。

我想从一个活动中的一个片段的Arraylist保存状态。基本上,这是一个JSON,由PHP脚本在ListFragment中填充List。然后,点击的项目从点击的项目发送名称和详细信息。

最后,当用户点击按钮“添加到购物车”时,这些数据将在第三个片段中的Arraylist处于同一个活动状态。

代码:

PedidosFragment.java

package com.example.waitersoriginal; 

*some imports* 

public class PedidosFragment extends ListFragment{ 

    public ArrayList<HashMap<String, String>> listaPedidos; 

    private static final String TAG_NM = "NOME"; 
    private static final String TAG_DS = "DESCR"; 

    ListAdapter adapter2; 

    public PedidosFragment(){} 

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     //abc = savedInstanceState; 

     super.onCreate(savedInstanceState); 
     restore(savedInstanceState); 

     listaPedidos = new ArrayList<HashMap<String, String>>(); 

     adapter2 = new SimpleAdapter(getActivity(), listaPedidos, 
       R.layout.list_item, 
       new String[] { TAG_NM, TAG_DS}, 
       new int[] { R.id.name, R.id.pid}); 

     setListAdapter(adapter2); 

     return inflater.inflate(R.layout.pedidos_fragment, container, false); 
    } 

    void criaArray(String nomeProd, String descrProd) { 


     HashMap<String, String> map3 = new HashMap<String, String>();   
     map3.put(TAG_NM, nomeProd); 
     map3.put(TAG_DS, descrProd); 

     listaPedidos.add(map3);  

      //getListView(); 
      ((BaseAdapter) adapter2).notifyDataSetChanged(); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putSerializable("listaPedidos", listaPedidos); 
    } 

    private void restore(Bundle savedInstanceState) { 
     if (savedInstanceState != null) { 
      listaPedidos = (ArrayList<HashMap<String,String>>) savedInstanceState.getSerializable("listaPedidos"); 
     } 
    } 

} 

当我离开的实际活动,并再次输入,ArrayList中已经没有了。我究竟做错了什么 ?

编辑:

更改onCreateView与给出的建议是:

public PedidosFragment(){} 

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     //abc = savedInstanceState; 

     super.onCreate(savedInstanceState);  

     listaPedidos = new ArrayList<HashMap<String, String>>(); 
     restore(savedInstanceState); 

     adapter2 = new SimpleAdapter(getActivity(), listaPedidos, 
       R.layout.list_item, 
       new String[] { TAG_NM, TAG_DS}, 
       new int[] { R.id.name, R.id.pid}); 

     setListAdapter(adapter2); 

     return inflater.inflate(R.layout.pedidos_fragment, container, false); 
    } 
+0

你离开的意思是什么?按下后退按钮? – ezcoding 2014-10-26 17:11:42

+0

的确如此,@DuZi .. – xxxxxx 2014-10-26 17:14:00

+0

如果你想在你的activity上调用'onDestroyed'后仍然保留这些项目,你必须将它们保存在设备上。您可以使用首选项,sqlite数据库或对象序列化来执行此操作。 – ezcoding 2014-10-26 17:16:36

回答

1

你是一个新的列表覆盖你恢复列表。将你的电话,以恢复该线下方你实例listaPedidos:

listaPedidos = new ArrayList<HashMap<String, String>>(); 
restore(savedInstanceState); 

然而,在他们的评论中提及@DuZi,破坏活动不会保存实例状态。从Android文档:

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

所以,你将需要处理的节约&加载这些数据自己。你可以把一个序列化对象转换成字符串偏好这样的:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
Editor editor = prefs.edit(); 
try { 
    editor.putString("listaPedidos", ObjectSerializer.serialize(listaPedidos)); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
editor.commit(); 

并重新加载数据是这样的:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 

try { 
    listaPedidos= (ArrayList<HashMap<String, String>>) ObjectSerializer.deserialize(prefs.getString("listaPedidos", ObjectSerializer.serialize(new ArrayList<HashMap<String, String>>()))); 
} catch (IOException e) { 
    e.printStackTrace(); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} 

您可以从Apache猪项目获得ObjectSerializer类:ObjectSerializer.java

+0

谢谢你的回答@Justin Powell!我已经做了你的建议,但是,当我按下“返回”按钮并重新输入活动时,数组列表仍然会更新。你有什么建议吗?我这样做(问题编辑) – xxxxxx 2014-10-26 17:46:06

+0

我已经更新了我的答案。 – 2014-10-26 18:06:11

+0

谢谢你和@DuZi的帮助!我在android开发中很新手,所以我对实现这个代码有点困惑..我创建了一个类ObjectSerializer ..我可能需要创建PreferenceManager吗?对不起,这个愚蠢的问题,谢谢! – xxxxxx 2014-10-26 18:34:56