0

I'm trying to make a "memory" to save three Dates + Hours in a StringArray. I 'll explain better with code. In my main activity I call this line:

conf.setDateArray(completeDate, conf.getPosition());

The two parameters are (a String, an int with the position of the array [0 or 1 or 2]). My problem comes in the Configuration class:

private String[] strDateArray = new String[3];
private int position = 0;

public void setDateArray(String str, int position) {
    SharedPreferences.Editor editor = getSettings().edit();
    // The problem is this code, I dont know how to write in the array in
    // the position
    this.strDateArray(position) = str(position);
    editor.putString(KEY_DATEARRAY, str);
    //
    editor.commit();
    if (position == 2) {
        this.position = 0;
    } else {
        this.position++;
    }
}

Thank you for your help!

EDIT: an example of what I want to do I want to introduce a date like "20-02-2014 14:14" to the StringArray[0] to let the user get this value when he starts again APP. My intention is to have the last three possible values, when all the array is filled, the position=0 will be overwritten.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
RaZieL
  • 63
  • 8
  • Can they not just be saved separately? Shared preferences are simply Key, Value pairs. You can change your array to a set and save it that way. See: http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – TEK Feb 18 '14 at 20:34
  • I'll edit my post with an example. – RaZieL Feb 18 '14 at 21:53
  • I formatted it but next time it will be a -1 – Mr_and_Mrs_D Feb 19 '14 at 19:34
  • Thank you for it, English is not my native language, so I have some mistakes. – RaZieL Feb 20 '14 at 10:11
  • @RaZieL: ok for english - no problem. But format your code (as I do mine) - in eclipse hit Ctrl+Shift+F :) – Mr_and_Mrs_D Feb 20 '14 at 11:33
  • I usually do this in eclipse, but I think my mistake was in Copy/Paste. Anyway, I'll keep your advise in mind and I'll format my code in next questions :) – RaZieL Feb 20 '14 at 11:49

1 Answers1

0
private static final String[] KEYS = { "0", "1", "2" };
private static final String POSITION = "LAST_POSITION";

public void setDateArray(String str) { // no need to pass the position in
    SharedPreferences prefs = getSettings(); // what's getSettings ?
    int pos = prefs.getInt(POSITION, 0); // get the last position - if you kept 
    // it in a field on app restart you will loose it
    final Editor editor = prefs.edit();
    editor.putString(KEYS[pos], str);
    pos = (pos + 1) % 3; // if pos + 1  == 3 with the %3 you get 0
    editor.putInt(POSITION, pos);
    editor.commit();
}

Although I am sure there is a simpler way to do this

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • In getSettings() it is returned: mContext.getSharedPreferences(SHARED_PREFS_FILE,Context.MODE_PRIVATE) Finally, I did it in "easier" for me, but not the optimal way. I separated the String in three different get/set methods. But your answer seems better and reduce considerably my code. Thanks a lot! – RaZieL Feb 20 '14 at 10:18