I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don't need the array available after the application has been closed completely. I save a lot of other objects this way by using the SharedPreferences but I can't figure out how to save my entire array this way. Is this possible? Maybe SharedPreferences isn't the way to go about this? Is there a simpler method?
当前回答
使用这个自定义类:
public class SharedPreferencesUtil {
public static void pushStringList(SharedPreferences sharedPref,
List<String> list, String uniqueListName) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(uniqueListName + "_size", list.size());
for (int i = 0; i < list.size(); i++) {
editor.remove(uniqueListName + i);
editor.putString(uniqueListName + i, list.get(i));
}
editor.apply();
}
public static List<String> pullStringList(SharedPreferences sharedPref,
String uniqueListName) {
List<String> result = new ArrayList<>();
int size = sharedPref.getInt(uniqueListName + "_size", 0);
for (int i = 0; i < size; i++) {
result.add(sharedPref.getString(uniqueListName + i, null));
}
return result;
}
}
使用方法:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferencesUtil.pushStringList(sharedPref, list, getString(R.string.list_name));
List<String> list = SharedPreferencesUtil.pullStringList(sharedPref, getString(R.string.list_name));
其他回答
public static void WriteSharePrefrence1(Context context, String key,
ArrayList<HashMap<String, String>> value)
{
final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(value);
editor.putString(key, json);
editor.commit();
}
public static ArrayList<HashMap<String, String>> ReadSharePrefrence1(Context context,
String key)
{
String data;
Gson gson = new Gson();
ArrayList<HashMap<String, String>> items = new ArrayList<>();
final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = preferences.edit();
data = preferences.getString(key, "");
Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {}.getType();
items = gson.fromJson(data, type);
return items;
}
请在kotlin中使用这两种方法将数据存储在ArrayList中
fun setDataInArrayList(list: ArrayList<ShopReisterRequest>, key: String, context: Context) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val editor = prefs.edit()
val gson = Gson()
val json = gson.toJson(list)
editor.putString(key, json)
editor.apply()
}
fun getDataInArrayList(key: String, context: Context): ArrayList<ShopReisterRequest> {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val gson = Gson()
val json = prefs.getString(key, null)
val type = object : TypeToken<ArrayList<ShopReisterRequest>>() {
}.type
return gson.fromJson<ArrayList<ShopReisterRequest>>(json, type)
}
我的utils类保存列表到SharedPreferences
public class SharedPrefApi {
private SharedPreferences sharedPreferences;
private Gson gson;
public SharedPrefApi(Context context, Gson gson) {
this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
this.gson = gson;
}
...
public <T> void putList(String key, List<T> list) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, gson.toJson(list));
editor.apply();
}
public <T> List<T> getList(String key, Class<T> clazz) {
Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();
return gson.fromJson(getString(key, null), typeOfT);
}
}
使用
// for save
sharedPrefApi.putList(SharedPrefApi.Key.USER_LIST, userList);
// for retrieve
List<User> userList = sharedPrefApi.getList(SharedPrefApi.Key.USER_LIST, User.class);
. 我的utils的完整代码//检查使用实例的活动代码
您可以将其转换为Map Object来存储它,然后在检索SharedPreferences时将值更改回ArrayList。
这应该可以工作:
public void setSections (Context c, List<Section> sectionList){
this.sectionList = sectionList;
Type sectionListType = new TypeToken<ArrayList<Section>>(){}.getType();
String sectionListString = new Gson().toJson(sectionList,sectionListType);
SharedPreferences.Editor editor = getSharedPreferences(c).edit().putString(PREFS_KEY_SECTIONS, sectionListString);
editor.apply();
}
他们,要抓住它才:
public List<Section> getSections(Context c){
if(this.sectionList == null){
String sSections = getSharedPreferences(c).getString(PREFS_KEY_SECTIONS, null);
if(sSections == null){
return new ArrayList<>();
}
Type sectionListType = new TypeToken<ArrayList<Section>>(){}.getType();
try {
this.sectionList = new Gson().fromJson(sSections, sectionListType);
if(this.sectionList == null){
return new ArrayList<>();
}
}catch (JsonSyntaxException ex){
return new ArrayList<>();
}catch (JsonParseException exc){
return new ArrayList<>();
}
}
return this.sectionList;
}
这对我很管用。
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?