【发布时间】:2019-12-12 17:02:43
【问题描述】:
在我的 Android 应用程序中,我将大量数据存档为我的 SharedPreferences 中的 json,json 项目按以下方式构建 {"codiceArticolo":"0401100028053","data":"mer 03/07/2019","qta":"1"} 在 JSON 中可能有超过 500 个这样的项目。
其实json是通过下面的方法从ArrayList生成的
public void saveStorico(){
ArrayList<ItemModel> itemToRemove = new ArrayList<>();
ArrayList<ItemModel> itemToAdd = new ArrayList<>();
// various controlls
for(ItemModel itemModels : itemModel){
boolean exist = false;
for(ItemModel itemModel2 : itemStorico){
if(itemModels.getCodiceArticolo().contains(itemModel2.getCodiceArticolo())) {
itemToRemove.add(itemModel2);
itemToAdd.add(itemModels);
exist = true;
}
}
if(!exist) {
itemToAdd.add(itemModels);
}
}
itemStorico.removeAll(itemToRemove);
itemStorico.addAll(itemToAdd);
SharedPreferences sharedPreferences = getSharedPreferences("STORICO_ORDINI", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(itemStorico);
editor.putString("storico", json);
editor.apply();
}
但是我在获取数据时遇到了一些问题,似乎某些已保存的项目没有被保存,是否有一个最大长度限制,我可以在 sharedpreferences 中作为 json 保存?
【问题讨论】:
标签: android json sharedpreferences