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

时间:2015-01-08 21:13:41

标签: java android android-fragments sqliteopenhelper

我正在设置一个Android应用,并且刚刚通过Android Developer文档来设置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”的按钮时,它会更新我的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数据库,因为我希望能够为多个按钮执行此操作。 但是,我也愿意接受任何其他实现这一目标的建议。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

如果您只想存储一个Integer,也许您可​​以查看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);

希望它对你有所帮助。