1. Are you ready for the Galaxy S20? Here is everything we know so far!

Storing and updating data

Discussion in 'Android Development' started by raym0nd, May 25, 2010.

  1. raym0nd

    raym0nd Lurker
    Thread Starter


    I want to create an app that requires reading/updating data. I wish to know which method suits the best i.e. store in phone, external storage or using SQLite.

    Basically, I need to store 3 different values which the value will be update regularly. I am thinking of creating a text file consist of either 3 lines or 1 line comma so that I can use array to split.

    Example > Data1, Data2, Data3

    However, I am not sure whether is this the best practice? Please advise.

    Thanks :)

    1. Download the Forums for Android™ app!


  2. icedog

    icedog Newbie

    I'd say an Xml file is a bit better than just writing the values to different files..

    SqlLite is a lot of overhead for three values.
  3. Boogs

    Boogs Member

    I wouldn't go with the XML file, because you said you need to update the values regularly.

    Let me ask you a question: are you going to be storing many sets of these multiple values? Or, do you just plan to update those same 3 values regularly?

    If that's the case, I would use SharedPreferences because of the low amount of overhead required, and the ease of use (don't have to deal with files, etc.). Here's a quick example:

    Code (Text):
    2. /* A name for your preferences file */
    3. public static final String PREFERENCES = "MyPreferences";
    5. /* Keys used for storing/retreiving values */
    6. public static final String KEY_AGE = "age";
    7. public static final String KEY_SEX = "sex";
    8. public static final String KEY_LOCATION = "location";
    10. /* Default values */
    11. public static final int DEFAULT_AGE = 21;
    12. public static final String DEFAULT_SEX = "Male";
    13. public static final String DEFAULT_LOCATION = "Antartica";
    15. protected void onCreate(Bundle state) {
    16.    super.onCreate(state);
    18.    ...
    20.    /* Get the information */  
    21.    SharedPreferences info = getSharedPreferences(PREFERENCES, 0);
    22.    int age = info.getInt(KEY_AGE, DEFAULT_AGE);
    23.    String sex = info.getString(KEY_SEX, DEFAULT_SEX);
    24.    String location = info.getString(KEY_LOCATION, DEFAULT_LOCATION);
    26.    /* Get new values for the information */
    27.    ...
    29.    /* Save some new information */
    30.    SharedPreferences.Editor editor = info.edit();
    31.    editor.putInt(KEY_AGE, age);
    32.    editor.putString(KEY_SEX, sex);
    33.    editor.putString(KEY_LOCATION, location);
    34.    editor.commit();
    35. }
    raym0nd likes this.
  4. raym0nd

    raym0nd Lurker
    Thread Starter

    Thanks for all replies :) Three values might be update regularly, I think SharedPreference might do the job well, gonna research on that. Thanks Boogs :)

Share This Page