android共享偏好灵活性

时间:2013-03-17 14:21:34

标签: android json sharedpreferences

有没有办法从SharedPreferences读取一个整数值作为字符串或反之? 就像将json值保存为整数时一样,您可以将其作为字符串读取。 通过这种方式,您可以避免在需要显示时将int解析为字符串,例如在TextView

2 个答案:

答案 0 :(得分:0)

SharedPreferences 使用xml来存储数据,而xml文件看起来非常像这样:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
  <int name="key1" value="0" />
  <boolean name="key2" value="true" />
  <string name="key3">example String</string>
 </map>

因此,android使用key查找具有该值的xml节点,并使用节点类型来验证针对请求的值。如果它们不匹配则抛出ClassCastException。 在JSON中,这样的验证不存在,解析器会尝试解析值,如果使用的是org.json对象,则会抛出JSONException

但是如果您想要实现JSON行为并且仍然可以轻松存储数据,则可以将其包装在JSONObject中,然后将其序列化为String并存储它。并且在检索时它再次将其解析为JSONObject,然后从中读取值。

答案 1 :(得分:0)

您可以编写一个帮助程序类来执行此操作。例如:

import android.content.Context;
import android.content.SharedPreferences;

/** Preferences (prefs) manager. */
public class Prefs
{
    public static final String MUSIC_KEY = "music_key";
    public static final String VOLUME_KEY = "volume_key";
    public static final String USER_ID_KEY = "user_id_key";
    public static final String FIRST_HELP_KEY = "first_help_key";
    public static final String SECOND_HELP_KEY = "second_help_key";
    public static final String CHALLENGE_HELP_KEY = "challenge_help_key";
    public static final String PREMIUM_KEY = "premium_key";
    public static final String GAMES_PLAYED_WHEN_STATS_SENT_KEY = "stats_games_played_key";

    private static final String FILENAME = "preferences";
    private static final SharedPreferences prefs = Const.context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);

    public static boolean getBoolean(String key, boolean defValue)
    {
        return prefs.getBoolean(key, defValue);
    }

    public static float getFloat(String key, float defValue)
    {
        return prefs.getFloat(key, defValue);
    }

    public static int getInt(String key, int defValue)
    {
        return prefs.getInt(key, defValue);
    }

    public static long getLong(String key, long defValue)
    {
        return prefs.getLong(key, defValue);
    }

    public static String getString(String key, String defValue)
    {
        return prefs.getString(key, defValue);
    }

    public static void putObject(String key, Object value)
    {
        final SharedPreferences.Editor editor = prefs.edit();
        if (value instanceof Boolean)
        {
            editor.putBoolean(key, (Boolean) value);
        }
        else if (value instanceof Float)
        {
            editor.putFloat(key, (Float) value);
        }
        else if (value instanceof Integer)
        {
            editor.putInt(key, (Integer) value);
        }
        else if (value instanceof Long)
        {
            editor.putLong(key, (Long) value);
        }
        else if (value instanceof String)
        {
            editor.putString(key, (String) value);
        }
        else
        {
            throw new IllegalArgumentException(value + " can't be inserted into SharedPreferences");
        }
        editor.commit();
    }

}

稍后,当您想要读取整数值时,您可以这样做:

final int lastSentGamesCounter = Prefs.getInt(Prefs.GAMES_PLAYED_WHEN_STATS_SENT_KEY, 0);

写作:

Prefs.putObject(Prefs.GAMES_PLAYED_WHEN_STATS_SENT_KEY, 10);