0

我不喜欢 AndroidSharedPreferences类的工作方式。我的主要原因是每次在应用程序的任何部分查找键值对时都必须指定默认值。鉴于大多数应用程序可能会在多个位置调用这些值,这种方法确实会在预期的应用程序/偏好行为中引发错误,因为没有中心点可以获取您的默认 设置。示例:活动 A getBoolean(key133, false),活动 B getBoolean(key133, true):问题。

我通过在 a 中指定/存储我所需的应用程序首选项值来规避这个问题HashMap,并且我有一个自定义派生类,它懒惰地在指定的 sSharedPreferences中查找默认值。HashMap我只是说一个 MyClass.getBoolean(key) 或 MyClass.getString(key),它会在 中查找默认值,HashMap如果它不存在,则返回并存储(.commit()-ing)它。默认行为是调用getBoolean(key, default_value). 这也有利于性能,因为在应用程序初始化时存储所有首选项在性能方面非常糟糕。

一个问题解决了,但现在我有了一个新问题:所有Preference派生类都找不到默认值,因为我无法使用自己的SharedPreference派生方式来查询默认的应用程序设置。这导致我的应用程序工作正常,但我的设置实际上并没有显示默认值,因为在第一次打开设置页面时它们还没有(总是)存储......

长话短说:我如何覆盖Preference课程以防止这种情况发生?

编辑:一些代码来解释问题以及我为什么选择这种方式

private final static Context context                    = ApplicationSubclass.getApplicationContext_();
private final static SharedPreferences prefs            = PreferenceManager.getDefaultSharedPreferences(context);  
private final static SharedPreferences.Editor editor    = prefs.edit();
public static Map<String, Boolean> booleans             = new HashMap<String, Boolean>();
public static Map<String, Integer> longs                = new HashMap<String, Integer>();
public static Map<String, String> strings               = new HashMap<String, String>();
public static Map<String, Integer> integers             = new HashMap<String, Integer>();

public aPreferences(){
    initialise();
}//end constructor

public void initialise(){
    // setup default preferences
    setBooleans();
    //setLongs();
    setStrings();
    //setIntegers();
}//end method   

private void setBooleans(){
    booleans.put("does_this_work", true);
    booleans.put("is_it_easy", true);
    booleans.put("is_it_free_of_problems", false);
    // and some hundreds more
}//end method

private void setStrings(){
    strings.put("show_me", "Show me!");
    // and some hundreds more
}//end method

// work the magic here
public boolean getBoolean(String key){
    if (!prefs.contains(key)) putBoolean(key, booleans.get(key));
    return prefs.getBoolean(key, booleans.get(key));
}//end method

public void putBoolean(String key, boolean value){
    editor.putBoolean(key, value).commit();
}//end method

这有效:

private static aPreferences values = new aPreferences();
if (values.getBoolean("does_this_work")) // value initialised upon first access
    Log.d(getClass().getSimpleName(), values.getString("show_me"));

这不会:

CheckBoxPreference sound = new CheckBoxPreference(this);
sound.setKey("does_this_work"); // value not initialised, so checkbox not checked
sound.setTitle(getResources().getString(R.string.sound_title));
sound.setSummary(getResources().getString(R.string.sound_text));
root.addPreference(sound);
4

1 回答 1

0

我实际上建议创建自己的首选项管理类来处理这样的事情。如果您需要知道默认值是什么,您可以设置自己的全局 get/set 方法并将默认值公开为公共静态字段。像这样的东西:

public class MyPreferences {

    private static final String INT_VALUE_1_KEY = "VALUE_1";
    public static final int INT_VALUE_1_DEFAULT = -1;

    public static void setDefaults(Context c) {
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
       Editor edit = prefs.edit();

       if (!prefs.contains(INT_VALUE_1_KEY)) {
          edit.putInt(INT_VALUE_1_KEY, INT_VALUE_1_DEFAULT);
       }
       // etc..

       edit.commit();
    }

    public static int getValue1(Context c) {
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
       return prefs.getInt(INT_VALUE_1_KEY, INT_VALUE_1_DEFAULT);
    }
}
于 2012-11-19T23:04:14.613 回答