How can I make the data entered in EditText of an activity saved when I leave and return to it? [closed]

1

We are a couple of high school students who try to create a small app for a job. Our problem is the following: after creating an activity that we intend to be a school schedule in which you can write down the subjects you have, we have had the problem that when writing the subjects in the said schedule, when leaving the app they do not they have saved and we have blank spaces again. How can we make these data entered saved? Thanks

    
asked by Joan Navarro 02.09.2016 в 17:25
source

1 answer

0

You can use several methods, from a text file, database or SharedPreferences, I think the simplest thing would be to use the latter.

You define a method to save the desired value:

public void saveValuePreference(Context context, String identificador, String text) {
    SharedPreferences settings = context.getSharedPreferences("mispreferencias", MODE_PRIVATE);
    SharedPreferences.Editor editor;
    editor = settings.edit();
    editor.putString(identificador, text);
    editor.commit();
}

and a method to obtain the values when you re-enter your application:

public String getValuePreference(Context context, String identificador) {
    SharedPreferences preferences = context.getSharedPreferences("mispreferencias", MODE_PRIVATE);
    return  preferences.getString(identificador, "");
}

Assuming you want to save the value of your EditText, we define an identifier called "miprimerdato", and as a last parameter you define the value of the EditText:

saveValuePreference(getApplicationContext, "miprimerdato", EditText .getText().toString());

to get the value again you define the identifier to get the preference data:

String valorEditText = getValuePreference(getApplicationContext(), "miprimerdato");
    
answered by 02.09.2016 в 18:15