【问题标题】:Store different data types in same shared preference [duplicate]将不同的数据类型存储在相同的共享首选项中[重复]
【发布时间】:2018-07-11 08:01:31
【问题描述】:

我是 android 新手,我想知道是否可以将布尔值和整数值存储在相同的共享首选项中。

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    是的,可以将所有数据类型存储在 sharedpreference 中:

    /******* 创建 SharedPreferences *******/

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
    Editor editor = pref.edit();
    

    /**************** 将数据存储为 KEY/VALUE 对 *******************/

    editor.putBoolean("key_name1", true);           // Saving boolean - true/false
    editor.putInt("key_name2", "int value");        // Saving integer
    editor.putFloat("key_name3", "float value");    // Saving float
    editor.putLong("key_name4", "long value");      // Saving long
    editor.putString("key_name5", "string value");  // Saving string
    
    // Save the changes in SharedPreferences
    editor.commit(); // commit changes
    

    /**************** 获取 SharedPreferences 数据 *******************/

    // 如果键的值不存在,则返回第二个参数值 - 在这种情况下为 null

    pref.getBoolean("key_name1", null);         // getting boolean
    pref.getInt("key_name2", null);             // getting Integer
    pref.getFloat("key_name3", null);           // getting Float
    pref.getLong("key_name4", null);            // getting Long
    pref.getString("key_name5", null);          // getting String
    

    /************ 从 SharedPreferences 中删除键值 *****************/

    editor.remove("key_name3"); // will delete key key_name3
    editor.remove("key_name4"); // will delete key key_name4
    
    // Save the changes in SharedPreferences
    editor.commit(); // commit changes
    

    /************ 清除 SharedPreferences 中的所有数据 *****************/

     editor.clear();
     editor.commit(); // commit changes
    

    【讨论】:

    • 如果我将具有相同键的不同数据类型存储在一个共享首选项中怎么办??
    • 不可能,Shared Preferences是一个键值对(键是唯一的)。您的代码所做的是替换之前保存的值。 @akshayashok
    【解决方案2】:
    /**
         * Save value to shared preference.
         *
         * @param key     On which key you want to save the value.
         * @param value   The value which needs to be saved.
         * @param context the context
         * @description To save the value to a preference file on the specified key.
         */
        public synchronized void saveValue(String key, long value, Context context) {
    
            SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor saveValue = prefs.edit();
            saveValue.putLong(key, value);
            saveValue.apply();
        }
    
        /**
         * Gets value from shared preference.
         *
         * @param key          The key from you want to get the value.
         * @param defaultValue Default value, if nothing is found on that key.
         * @param context      the context
         * @return the value from shared preference
         * @description To get the value from a preference file on the specified
         * key.
         */
        public synchronized String getValue(String key, String defaultValue, Context context) {
    
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            return prefs.getString(key, defaultValue);
        }
    

    虽然如果您想将 POJO 保存在共享首选项中,请选择 GSON

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      相关资源
      最近更新 更多