I need to create an array of the last 5 call dates.
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
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?
Here what I'm doing but is throwing errors everywhere:
SAVE
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();
READ:
String lastCall = callLogPreferences.getString("lastCall", null);
JSONDeserializer<List<String>> der = new JSONDeserializer<>();
List<String> textList = der.deserialize(lastCall);
I'm using FlexJson: GSON and InstanceCreator issue
It's not converting from string to Array List
Try using an
ArrayList
with a customComparator
as seen here: Sort ArrayList of custom Objects by propertyThen, sort the
ArrayList
accordingly.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.
So from your question you want to store a
List<String>
myList insideSharedPreferences
. To do so I advise you to useGson
because it's simple. The main idea it's to serialize yourList
to aString
injson
and then store thatString
:Since you are using primitive types you don't need to specify a TypeToken so you just need to:
And it's stored. To retrieve the list just do the opposite:
And that's it!
Hope it helped.
ps:
SharedPreferences
it's used to store values that can be accessed in otherClasses
/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.EDIT: this lib may save you some time with the serialization ;)