Fil*_*eOS 2 java arrays android sharedpreferences flexjson
我需要创建一个包含最近 5 个通话日期的数组。
我知道我必须将日期保存在数组中,但我不知道如何重新排序其他记录以将最后一次调用日期保留为第一个记录。并且始终只维护5条记录
目标是保存最后一个调用字符串并添加到数组中,之后我重新排序并仅维护 5 条记录,之后我使用 FlexJson 制作一个字符串并保存在共享首选项上。谁有这个完整的流程吗?
这是我正在做的事情,但到处都抛出错误:
节省
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
String currentDateTimeString = df.format(calendar.getTime());
List<String> textList = new ArrayList<>();
JSONSerializer ser = new JSONSerializer();
textList.add(currentDateTimeString);
String jsonText = ser.deepSerialize(textList);
editor.putString("lastCall", jsonText);
editor.commit();
Run Code Online (Sandbox Code Playgroud)
读:
String lastCall = callLogPreferences.getString("lastCall", null);
JSONDeserializer<List<String>> der = new JSONDeserializer<>();
List<String> textList = der.deserialize(lastCall);
Run Code Online (Sandbox Code Playgroud)
我正在使用 FlexJson:GSON 和 InstanceCreator 问题
它没有从字符串转换为数组列表
首先让我给你一个建议 - 如果你有 3 个问题,请创建 3 个主题,而不是一个包含所有问题的主题。根据问题标题:Android array to sharedpreferences,我将针对这个问题给出答案。
所以从你的问题来看,你想在里面存储一个List<String>myList SharedPreferences。为此,我建议您使用,Gson因为它很简单。主要思想是将您的序列化为inList然后存储:StringjsonString
由于您使用的是原始类型,因此不需要指定 TypeToken ,因此您只需要:
public static final MY_LIST = "my_list";
List<String> myList = new ArrayList<String>();
SharedPreferences.Editor editor = prefs.edit();
editor.putString(MY_LIST, new Gson().toJson(myList));
editor.commit();
Run Code Online (Sandbox Code Playgroud)
并且它被存储了。要检索列表,只需执行相反的操作:
myList = new Gson().fromJson(prefs.getString(MY_LIST, null), List<String>);
Run Code Online (Sandbox Code Playgroud)
就是这样!
希望有帮助。
ps:SharedPreferences它用于存储可以在其他Classes/ Activities/等中访问的值,或者在应用程序重新启动之间保留信息。您不需要这个来完成您想要的事情:创建一个包含 5 个有序字符串的列表。
编辑:这个库可以为您节省一些序列化时间;)
| 归档时间: |
|
| 查看次数: |
2612 次 |
| 最近记录: |