如何在数据库中存储首选项?

时间:2013-06-06 14:24:11

标签: android android-preferences

根据文档,有几种方法可以存储内容。其中一个是数据库,其中一个是通过 SharedPreferences

对于我的应用程序,我已经使用了一个数据库,因此认为最好的方法是将所有内容存储在一个地方,即将我的首选项存储在数据库中。搜索,我只找到与 SharedPreferences 相关的示例。在xml文件中定义首选项结构具有易于维护的优点,但缺点是背景颜色与子菜单/子屏幕有关。

  1. SharedPreferences 的方法是否适用于我想做或做的事情,我需要创建自己的结构(默认值)和布局resp。查看(用于演示)?

  2. 是否有人知道使用PreferenceActivity的有效样本来保存数据库中的设置? R.xml.preferences ==> PreferenceScreen

2 个答案:

答案 0 :(得分:2)

我会将偏好存储在SharedPreferences中 - 这就是它们的用途,它们有一整套类和方法。您当然可以将它们存储在您自己的数据库中,但您必须设置表,编写适配器等等。有什么好处?

我的很多应用程序都有一个sqlite数据库,用于存储内容等。我存储在SharedPreferences中的首选项。

答案 1 :(得分:0)

环顾四周后,我发现了sample,最终实施了PreferenceActivity。基于此,我删除了不需要的东西,剩下的就是其余部分(特别是DB-update resp。为单个实现的读取值。但是,如果不是这些,我认为这不会太难。

public class MyPreferences extends PreferenceActivity {

  public class CursorPreferenceHack implements SharedPreferences {

    protected Map<String, Object> values = new HashMap<String, Object>();
    private DBPreferences         dbPreferences;

    public CursorPreferenceHack() {
      dbPreferences = new DBPreferences();
      values = dbPreferences.cacheValues();
    }

    public boolean contains(String key) {
      return values.containsKey(key);
    }

    public class Editor implements SharedPreferences.Editor {

      private ContentValues update = new ContentValues();

      public SharedPreferences.Editor clear() {
        update = new ContentValues();
        return this;
      }

      public boolean commit() {
        dbPreferences.updateValues(update);
        // make sure we refresh the parent cached values
        values = dbPreferences.cacheValues();

        // and update any listeners
        for (OnSharedPreferenceChangeListener listener : listeners) {
          listener.onSharedPreferenceChanged(CursorPreferenceHack.this, null);
        }

        return true;
      }

      // Gingerbread compatibility
      public void apply() {
        commit();
      }

      public android.content.SharedPreferences.Editor putBoolean(String key,
                                                                 boolean value) {
        return this.putString(key, Boolean.toString(value));
      }

      public android.content.SharedPreferences.Editor putFloat(String key,
                                                               float value) {
        return this.putString(key, Float.toString(value));
      }

      public android.content.SharedPreferences.Editor putInt(String key,
                                                             int value) {
        return this.putString(key, Integer.toString(value));
      }

      public android.content.SharedPreferences.Editor putLong(String key,
                                                              long value) {
        return this.putString(key, Long.toString(value));
      }

      public android.content.SharedPreferences.Editor putString(String key,
                                                                String value) {
        update.put(key, value);
        return this;
      }

      public android.content.SharedPreferences.Editor remove(String key) {
        update.remove(key);
        return this;
      }

      public android.content.SharedPreferences.Editor putStringSet(String key,
                                                                   Set<String> value) {
        throw new UnsupportedOperationException("HostEditor Prefs do not support Set<String>");
      }
    }

    public Editor edit() {
      return new Editor();
    }

    public Map<String, ?> getAll() {
      return values;
    }

    public boolean getBoolean(String key, boolean defValue) {
      return Boolean.valueOf(this.getString(key, Boolean.toString(defValue)));
    }

    public float getFloat(String key, float defValue) {
      return Float.valueOf(this.getString(key, Float.toString(defValue)));
    }

    public int getInt(String key, int defValue) {
      return Integer.valueOf(this.getString(key, Integer.toString(defValue)));
    }

    public long getLong(String key, long defValue) {
      return Long.valueOf(this.getString(key, Long.toString(defValue)));
    }

    public String getString(String key, String defValue) {
      if (!values.containsKey(key))
        return defValue;
      return (String) values.get(key);
    }

    public Set<String> getStringSet(String key, Set<String> defValue) {
      throw new ClassCastException("HostEditor Prefs do not support Set<String>");
    }

    protected List<OnSharedPreferenceChangeListener> listeners = new LinkedList<OnSharedPreferenceChangeListener>();

    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
      listeners.add(listener);
    }

    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
      listeners.remove(listener);
    }

  }

  @Override
  public SharedPreferences getSharedPreferences(String name, int mode) {
    return this.pref;
  }

  private CursorPreferenceHack  pref;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    this.pref = new CursorPreferenceHack();
    // this.pref.registerOnSharedPreferenceChangeListener(this);

    this.addPreferencesFromResource(R.xml.preferences);
  }
}

对于cacheValues

public Map<String, Object> cacheValues() {
    Map<String, Object> retValues = new HashMap<String, Object>();
       SQLiteDatabase db = dbManager.getReadableDatabase();
    Cursor cursor = db.query(dbTblPref, new String[] { "pref_key", 
        "pref_value" }, null, null, null, null, null);

    if (cursor.moveToFirst()) {
      do {

        retValues.put(cursor.getString(0),cursor.getString(1));
      } while (cursor.moveToNext());
    }
    cursor.close();

    return retValues;
  }