Android的阵列与GSON共享preferences [英] Android array to Sharedpreferences with Gson
问题描述
我需要创建的最后5个呼叫日期的数组。
I need to create an array of the last 5 call dates.
我知道我有救阵列上的日期,但我不知道如何重新安排其他记录保留这最后赎回日第一的纪录。始终保持只有5条记录
I know that I have to save the date on the array but I don't know how to reorder the other records to keep this last call date the 1st record. And always maintain only 5 records
目标是保存了最近一个电话串并添加到数组,在那之后我重新排序并保持只有5条记录,在那之后我用FlexJson做一个字符串,并保存在共享preferences。任何人有这个全过程的?
The Goal is to save that last call string and add to the array, after that I reorder to and maintain only the 5 records, after that I use FlexJson to make a string and save on sharedpreferences. Anyone have this full process?
在这里,我在做什么,但随处乱扔错误:
保存
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();
阅读:
String lastCall = callLogPreferences.getString("lastCall", null);
JSONDeserializer<List<String>> der = new JSONDeserializer<>();
List<String> textList = der.deserialize(lastCall);
我使用FlexJson我:<一href=\"http://stackoverflow.com/questions/20447897/gson-and-instancecreator-issue/20448172#20448172\">GSON和InstanceCreator问题
这不是从字符串转换为数组列表
It's not converting from string to Array List
推荐答案
首先,让我给你一个提醒 - 如果你有3提问创建3个主题,而不是一个与所有的问题。基于问题的标题为 Android的阵列共享preferences 我就这个问题给出一个答案
First let me give you an advise - if you have 3 question create 3 topics, not one with all the questions. Based on the question title: Android array to sharedpreferences I'll give an answer just to this issue.
所以,从你的问题,你要存储一个列表&LT;弦乐&GT;
myList中在共享preferences
。要做到这一点,我建议你使用 GSON
,因为它很简单。其主要思想是序列化你的列表
到字符串
在 JSON
然后存储字符串
:
So from your question you want to store a List<String>
myList inside SharedPreferences
. To do so I advise you to use Gson
because it's simple. The main idea it's to serialize your List
to a String
in json
and then store that String
:
由于您使用的基本类型,你不必指定<一个href=\"http://stackoverflow.com/questions/14705860/saving-serializable-objects-list-into-shared$p$pferences/16607928#16607928\">TypeToken所以你只需要:
Since you are using primitive types you don't need to specify a TypeToken so you just need to:
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();
和它的存储。要检索列表只是做相反的:
And it's stored. To retrieve the list just do the opposite:
myList = new Gson().fromJson(prefs.getString(MY_LIST, null), List<String>);
这就是它!结果
希望它帮助。
And that's it!
Hope it helped.
PS:共享preferences
它用于可在其他类访问存储值
/ 活动
/等或将持续重新启动应用程序之间的信息。你并不需要用它来完成你想要的:创建5有序的字符串列表
ps: SharedPreferences
it's used to store values that can be accessed in other Classes
/Activities
/etc.. or to persist information between app restarts. You don't need this to accomplish what you want: Create a list with 5 ordered strings.
这篇关于Android的阵列与GSON共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!