如何在 Android 中的 Bundle 中存储嵌套的 Arraylist
How to store nested Arraylists in a Bundle in Android
我的 Android 应用程序源代码中有这个对象:
ArrayList<ArrayList<MyObject>> rsp
我想将它存储在一个 Bundle 中并在我的 activity 中的 OnSaveInstanceState()
方法中使用它。然后我也想从捆绑包中取回它。
我应该使用 json 吗?还有别的办法吗?
如果可能使用Gson
库
Gson gson = new Gson();
String output = gson.toJson(rsp); // Create String and pass through bundle
从字符串中检索该列表
//convert from string
String output = // get that string from bundle
ArrayList<ArrayList<MyObject>> fromString = gson.fromJson(output,new TypeToken<List<ArrayList<ArrayList<MyObject>>>>(){}.getType());
您可以在 OnSaveInstanceState() 期间使用 Parcelable 将数据存储在 Bundle 中
使用下面的 link 了解更多详情:
How to save custom ArrayList on Android screen rotate?
我的 Android 应用程序源代码中有这个对象:
ArrayList<ArrayList<MyObject>> rsp
我想将它存储在一个 Bundle 中并在我的 activity 中的 OnSaveInstanceState()
方法中使用它。然后我也想从捆绑包中取回它。
我应该使用 json 吗?还有别的办法吗?
如果可能使用Gson
库
Gson gson = new Gson();
String output = gson.toJson(rsp); // Create String and pass through bundle
从字符串中检索该列表
//convert from string
String output = // get that string from bundle
ArrayList<ArrayList<MyObject>> fromString = gson.fromJson(output,new TypeToken<List<ArrayList<ArrayList<MyObject>>>>(){}.getType());
您可以在 OnSaveInstanceState() 期间使用 Parcelable 将数据存储在 Bundle 中
使用下面的 link 了解更多详情:
How to save custom ArrayList on Android screen rotate?