1. Check out our companion app, Forums for Android! Download from Google Play

Storing and updating data

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

  1. raym0nd

    raym0nd Member
    Thread Starter
    5

    May 12, 2010
    5
    0
    5
    Hi,

    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 :)
     

    Advertisement

  2. icedog

    icedog Member
    16

    Apr 28, 2010
    23
    9
    16
    Software Engineer
    Silicon Valley
    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 Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    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):
    1.  
    2. /* A name for your preferences file */
    3. public static final String PREFERENCES = "MyPreferences";
    4.  
    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";
    9.  
    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";
    14.  
    15. protected void onCreate(Bundle state) {
    16.    super.onCreate(state);
    17.  
    18.    ...
    19.  
    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);
    25.  
    26.    /* Get new values for the information */
    27.    ...
    28.  
    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. }
    36.  
     
    raym0nd likes this.
  4. raym0nd

    raym0nd Member
    Thread Starter
    5

    May 12, 2010
    5
    0
    5
    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

Loading...