0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AndroidのPreference系を簡単に扱いたい

Posted at

##Preference系
SQLiteを使うまでもないデータを保存しておくにはPreferenceを使うのが一番です。

しかしながら、一つのデータにアクセスするコードが微妙に長い。超せっかっち人間の僕にとっては悩みの種でした。

##簡単に扱えるようにした?
簡単?に扱えるようにクラスにしてみました。インスタンスはアプリで一つ。トップレベルで保持することとします。

##コード

lass SettingsClass(_context: Context) {
    private val context = _context

    fun setBoolean(name: String, key: String, value: Boolean) {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        preference.edit().putBoolean(key, value).apply()
    }

    fun getBoolean(name: String, key: String, default: Boolean): Boolean {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return preference.getBoolean(key, default)
    }

    fun setInt(name: String, key: String, value: Int) {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        preference.edit().putInt(key, value).apply()
    }

    fun getInt(name: String, key: String, default: Int): Int {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return preference.getInt(key, default)
    }

    fun setLong(name: String, key: String, value: Long) {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        preference.edit().putLong(key, value).apply()
    }

    fun getLong(name: String, key: String, default: Long): Long {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return preference.getLong(key, default)
    }

    fun setString(name: String, key: String, value: String?) {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        preference.edit().putString(key, value).apply()
    }

    fun getString(name: String, key: String, default: String?): String? {
        val preference = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return preference.getString(key, default)
    }

    fun pSetBoolean(key: String, value: Boolean) {
        val preference = PreferenceManager.getDefaultSharedPreferences(context)
        preference.edit().putBoolean(key, value).apply()
    }

    fun pGetBoolean(key: String, default: Boolean): Boolean {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, default)
    }

    fun pSetInt(key: String, value: Int) {
        val preference = PreferenceManager.getDefaultSharedPreferences(context)
        preference.edit().putInt(key, value).apply()
    }

    fun pGetInt(key: String, default: Int): Int {
        return PreferenceManager.getDefaultSharedPreferences(context).getInt(key, default)
    }

    fun pSetString(key: String, value: String) {
        val preference = PreferenceManager.getDefaultSharedPreferences(context)
        preference.edit().putString(key, value).apply()
    }

    fun pGetString(key: String, default: String): String? {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(key, default)
    }

    companion object {
        const val SETTING_MUSIC_MODE = "CAIOS_SETTING_MUSIC_MODE"
        const val SETTING_MUSIC_VALUE = "CAIOS_SETTING_MUSIC_VALUE"
    }

最後のcompanion objectの値は自由です。アプリに沿って決めてください。

##使用例
設定値の保存

settings.setBoolean("設定-bool", true)

設定値の読み出し

settings.getInt("設定-int", -1)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?