将整数存储在 SQL 数据库中并在片段中检索它

Store Integer in SQL Database & Retrieve it in Fragment

我正在设置一个 Android 应用程序,并且刚刚阅读了 Android 开发人员文档以设置一个 SQL 数据库 http://developer.android.com/training/basics/data-storage/databases.html#WriteDbRow

我有一个 MainActivity,它设置了导航抽屉:http://pastebin.com/9nyejZdV,其中的一个开关 activity 用于在单击项目时替换片段在导航抽屉中。

切换的两个片段是:

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


在 StartingFragment 中,在 onClick 方法中,只要按下名为 "Oolong" 的按钮,它就会更新我的 TeaCounterFragment 中的 TextView,显示 "Oolong" 按钮被按下的次数(通过使用 String.valueOf(an_integer) 和捆绑包)。

问题是每当应用程序完全关闭时,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 数据库,因为我希望能够为多个按钮执行此操作。 但是,我也乐于接受有关实现此目标的任何其他方法的建议。

由于您只想存储一个整数,因此您应该使用共享首选项而不是数据库。

如果您只想存储一个整数,也许您可​​以看看 DefaultSharedPreferences class。看到这个link: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);

希望对您有所帮助。