我有一个“ArrayList> List_Data =new ArrayList>();”
但我的要求是将该 List_Data 存储到 sharedpreference 中。请帮助我。
最佳答案
将您的数组或对象转换为 JSON 并存储到共享首选项中
用于存储:
SharedPreferences db=PreferenceManager.getDefaultSharedPreferences(context);
Editor collection = db.edit();
Gson gson = new Gson();
String arrayList1 = gson.toJson(arrayList);
collection.putString(key, arrayList1);
collection.commit();
用于检索
SharedPreferences db=PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String arrayListString = db.getString(key, null);
Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType();
ArrayList<ArrayObject> arrayList = gson.fromJson(arrayListString, type);
关于android - 如何将 ArrayList<HashMap<String, String>> 数据存储到 sharedpreferences 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523898/