I'm setting up an Android app and have just gone through the Android Developer documentation to set up an SQL Database http://developer.android.com/training/basics/data-storage/databases.html#WriteDbRow
I have a MainActivity that has the navigation drawer set up: http://pastebin.com/9nyejZdV, and a switch within that activity to replace fragments when items are clicked in the nav drawer.
The two fragments that are switched are:
StartingFragment, which is displayed when the app is first opened: http://pastebin.com/740LepNV
TeaCounterFragment, which is displayed when the second item in my navdrawer is clicked: http://pastebin.com/edfEe1Gd
In StartingFragment, in the onClick method, whenever a button called "Oolong" is pressed, it updates a TextView in my TeaCounterFragment, that displays how many times the "Oolong" button has been pressed (by using String.valueOf(an_integer), and a Bundle).
The problem is that whenever the app is completely closed, the TextView resets to default, which has no count displayed. So, I figured that I would use an SQL Database to store the integer, so that when the app is completely closed, the integer will still be stored. Then, when the app is opened back up, I can click on my NavDrawer and go to my TeaCounterFragment, and the number will still be there.
How can I achieve this?
Database Activity:
public final class Database {
// To prevent someone from accidentally instantiating the database class,
// give it an empty constructor.
public Database() {
}
/* Inner class that defines the table contents */
public static abstract class DBEntry implements BaseColumns {
public static final String TABLE_NAME = "Number of Cups of Tea";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "Tea Counter Table";
public static final String COLUMN_NAME_SUBTITLE_OOLONG = "Oolong";
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + DBEntry.TABLE_NAME + " (" +
DBEntry._ID + " INTEGER PRIMARY KEY," +
DBEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
DBEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
DBEntry.COLUMN_NAME_SUBTITLE_OOLONG + TEXT_TYPE + COMMA_SEP +
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS" + DBEntry.TABLE_NAME;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "TeaCounter.db";
public static final int DATABASE_VERSION = 1;
Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES); //creates Database
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public void storeCounts() {
DatabaseHelper mDbHelper = new DatabaseHelper(context);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(DBEntry.COLUMN_NAME_ENTRY_ID, 1);
values.put(DBEntry.COLUMN_NAME_TITLE, "Tea Counter Table");
values.put(DBEntry.COLUMN_NAME_SUBTITLE_OOLONG, "Oolong Tea");
}
}
}
I thought of using an SQL Database because I would like to be able to do this for multiple buttons. However, I'm also open to suggestions of any other way of achieving this.
If you just want to store a Integer, maybe you could take a look to the DefaultSharedPreferences class. See this link : http://developer.android.com/reference/android/content/SharedPreferences.html
if you are doing this from your fragment:
SharedPreferences pref = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("counter", integer);
editor.commit();
Then when you want to retrieve it, assuming you already have the SharedPreferences instance
Integer counter = pref.getInt("counter", 0);
Hope it helps you.