我正在设置一个Android应用程序,并且刚刚浏览了Android文档来设置一个SQL数据库http://developer.android.com/training/basics/data-storage/databases.html#WriteDbRow
我有一个MainActivity,它设置了导航抽屉:http://pastebin.com/9nyejZdV,并在该活动中设置了一个开关,以在导航抽屉中单击项目时替换片段。
切换的两个片段是:
当应用程序第一次打开时显示的StartingFragment,:http://pastebin.com/740LepNV
TeaCounterFragment,,它在单击导航抽屉中的第二项时显示:http://pastebin.com/edfEe1Gd
在StartingFragment中,在onClick方法中,每当按下一个名为"Oolong“的按钮时,它就会在method中更新一个TextView,显示按下"Oolong”按钮的次数(使用String.valueOf(an_integer)和Bundle)。
问题是,每当应用程序完全关闭时,TextView会将其重置为默认设置,这将不显示计数。因此,我想我应该使用SQL数据库来存储整数,这样当应用程序完全关闭时,整数仍然会被存储。然后,当应用程序被重新打开时,我可以点击我的NavDrawer并转到我的TeaCounterFragment,这个号码还会在那里。
我怎样才能做到这一点?
数据库活动:
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");
}
}
}我考虑使用SQL数据库,因为我希望能够对多个按钮这样做。然而,我也愿意接受任何其他方法的建议来实现这一点。
发布于 2015-01-08 21:29:58
如果您只想存储一个整数,也许可以查看一下DefaultSharedPreferences类。请参阅此链接:http://developer.android.com/reference/android/content/SharedPreferences.html
如果您是从片段中执行此操作的:
SharedPreferences pref = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("counter", integer);
editor.commit();然后当您想要检索它时,假设您已经拥有了SharedPreferences实例
Integer counter = pref.getInt("counter", 0);希望它能帮到你。
发布于 2015-01-08 21:22:15
由于您只想存储一个整数,所以应该使用共享首选项而不是数据库。
https://stackoverflow.com/questions/27849608
复制相似问题