Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save 2 strings on an android app?

Tags:

java

android

save

I'm a relatively new programmer, and I haven't really worked with xml before other than writing layouts for android applications.

I want to be able to have a user save an int value (figured it'd be easier to save the int as a string, however), so that way it is usable whenever they use my app. (For example, I have a start time and end time I want them to be able to save instead of inserting everytime they open the app).

I was thinking the easiest way to go about this would be to save the file to an xml file, but after looking at android tutorials, the only coding I could understand about xml files was how to load the resource files from the files, but nothing on how to save (or edit) these strings.

I don't have access to a database for this project, so it needs to be able to be saved directly on the phone.

What's the easiest way to save 2 strings for an android app?

like image 662
Jeff Avatar asked Nov 28 '10 05:11

Jeff


2 Answers

Look at SharedPreferences. You ask for a shared preferences by name, which creates it if it doesn't exist, you then get an editor to it, then add some named values to it.

At some other point you get the shared preferences and pull your values out by name, you can also add a listener to it so that you are notified of changes to your shared preferences object.

Getting your shared prefs:

SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);

Saving a value:

SharedPreferences.Editor e = myPrefs.edit();
e.putInt("someValue", 50); // add or overwrite someValue
e.commit(); // this saves to disk and notifies observers

Retrieving a value:

int i = myPrefs.getInt("someValue", 0); // return 0 if someValue doesn't exist
like image 159
Gareth Davidson Avatar answered Sep 23 '22 12:09

Gareth Davidson


You should use SharedPreferences to achieve that, in something like this:

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}

Just in your situation you should use the get and put methods for String and for Integer This example and more information can be found at Android Developer website here.

like image 39
Luis Miguel Serrano Avatar answered Sep 22 '22 12:09

Luis Miguel Serrano