0

我的 Android App 源代码中有这个对象:

ArrayList<ArrayList<MyObject>> rsp

我想将它存储在一个Bundle中并OnSaveInstanceState()在我的活动中使用它。然后我也想从包中检索它。

我应该使用json吗?还有其他方法吗?

4

2 回答 2

1

如果可能的话使用Gson图书馆

 Gson gson = new Gson();
    String output = gson.toJson(rsp); // Create String and pass through bundle

从 String 中检索该列表

    //convert from string
String output =  // get that string from bundle
    ArrayList<ArrayList<MyObject>> fromString = gson.fromJson(output,new TypeToken<List<ArrayList<ArrayList<MyObject>>>>(){}.getType());
于 2016-12-28T10:43:23.140 回答
1

您可以在 OnSaveInstanceState() 期间使用 Parcelable 将数据存储在 Bundle 中

使用以下链接了解更多详情:

如何在 Android 屏幕旋转上保存自定义 ArrayList?

于 2016-12-28T11:26:08.353 回答