How to store in a list the last 5 opening dates of an Android activity

0
 public class Activity extends AppCompatActivity {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss");
    String strDate = sdf.format(c.getTime());

            @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity);
              EscreverData();}

           private void EscreverData(){
              TextView datas = (TextView) findViewById(R.id.datas);
              datas.append(strDate);}}

Here is my code, but, as you can see, the only thing it does is to display, in TextView, the date of entry, of that moment, in the activity. What I really wanted to do was to present the last 5 dates of the activity in TextView without disappearing when I left the application, that is, store them in a list ... If anyone can help me thank you

    
asked by anonymous 13.08.2017 / 21:43

1 answer

0
public class Activity extends AppCompatActivity {
        public static final String PREFS_NAME = "Preferences";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss");
        String strDate = sdf.format(c.getTime());

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        TextView datas = (TextView) findViewById(R.id.datas);
        datas.setMaxLines(5);
//Restaura as preferencias gravadas
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_APPEND);
        String a = settings.getString("PrefUsuario", "");
        datas.append(strDate + "\n" + a);}

/**Chamado quando a Activity é encerrada.*/
@Override
protected void onStop(){
        super.onStop();
        TextView datas = (TextView) findViewById(R.id.datas);
        String a = datas.getText().toString();
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_APPEND);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("PrefUsuario", a);
//Confirma a gravação dos dados
        editor.commit();}}

Here, I think, is the solution to my own question - Through SharedPreferences

    
14.08.2017 / 12:44