• 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
May 12, 2010
5
0
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 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();
}
 
  • Like
Reactions: raym0nd
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones