• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Storing and updating data

raym0nd

Lurker
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 :)
 
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.
 
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:
/* A name for your preferences file */
public static final String PREFERENCES = "MyPreferences";
 
/* Keys used for storing/retreiving values */
public static final String KEY_AGE = "age";
public static final String KEY_SEX = "sex";
public static final String KEY_LOCATION = "location";
 
/* Default values */
public static final int DEFAULT_AGE = 21;
public static final String DEFAULT_SEX = "Male";
public static final String DEFAULT_LOCATION = "Antartica";
 
protected void onCreate(Bundle state) {
   super.onCreate(state);
 
   ...
 
   /* Get the information */   
   SharedPreferences info = getSharedPreferences(PREFERENCES, 0);
   int age = info.getInt(KEY_AGE, DEFAULT_AGE);
   String sex = info.getString(KEY_SEX, DEFAULT_SEX);
   String location = info.getString(KEY_LOCATION, DEFAULT_LOCATION);
 
   /* Get new values for the information */
   ...
 
   /* Save some new information */
   SharedPreferences.Editor editor = info.edit();
   editor.putInt(KEY_AGE, age);
   editor.putString(KEY_SEX, sex);
   editor.putString(KEY_LOCATION, location);
   editor.commit();
}
 
Thanks for all replies :) Three values might be update regularly, I think SharedPreference might do the job well, gonna research on that. Thanks Boogs :)
 
Back
Top Bottom