2

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

Community
  • 1
  • 1
FilipeOS
  • 801
  • 1
  • 11
  • 42
  • See here: http://stackoverflow.com/questions/14705860/saving-serializable-objects-list-into-sharedpreferences/16607928#16607928 – GuilhE Jun 04 '15 at 21:24
  • Hi @GuilhE if you check the link on my question you will see that FlexJson don't require a InstanceCreator, I used GSON but it was throwing that error. – FilipeOS Jun 04 '15 at 21:42
  • I find Gson more straightforward. Can you post you Gson serialize code and error? – GuilhE Jun 04 '15 at 21:46
  • Hi @GuilhE I don't have it, I've seen in some stackoverflow topic. Can you show me an answer how to make a array list into string and then the string to array list with GSON? Thank you – FilipeOS Jun 04 '15 at 22:04
  • Yes of course, see my answer. – GuilhE Jun 04 '15 at 22:18

2 Answers2

5

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 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:

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: 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.

EDIT: this lib may save you some time with the serialization ;)

GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • Hi, so I need to .add the strings to that myList right? ( myList.add("x"); ) – FilipeOS Jun 04 '15 at 22:41
  • Yes, the code I gave you here it's just an example on how to store a List inside SharedPreferences. If the List has elements, ordered or not, or it's empty it's up to you. – GuilhE Jun 04 '15 at 22:42
  • dateList = new Gson().fromJson(callLogPreferences.getString("lastCall", null), new TypeToken>(){ ---> make the job on your last code tip! THANK YOU – FilipeOS Jun 05 '15 at 12:42
1

Try using an ArrayList with a custom Comparator as seen here: Sort ArrayList of custom Objects by property

Then, sort the ArrayList accordingly.

Community
  • 1
  • 1
Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64
  • No sorry, the above link is more than sufficient for what I believe you are asking. I am not here to do your job :). However if you feel my answer has not been sufficient, please extend details or clarify for me and I will do my best to reply with additional information/correct what I have said. – Timothy Frisch Jun 04 '15 at 20:40
  • Hi there, okay no problem, I'm a noob and your answer is like nothing to me :( sorry. If you cannot make a better example I understand – FilipeOS Jun 04 '15 at 20:50
  • From what I understand you are writing the `JSON` above to `sharedPreferences`, then attempting to retrieve the `JSON` data back. Correct? – Timothy Frisch Jun 04 '15 at 20:52
  • Yes, some guy told me that I need to convert the array to string with json, save with sharedpreferences and then convert the json string into array again to read the data and place on that expandable listview. But it's not working :( I've the 3 topics about what is not working – FilipeOS Jun 04 '15 at 21:03