android共享偏好灵活性
有没有办法从SharedReferences中读取一个整数值作为string或viceversa? 就像当您将json值保存为整数时,您可以将其读取为字符串。android共享偏好灵活性,android,json,sharedpreferences,Android,Json,Sharedpreferences,有没有办法从SharedReferences中读取一个整数值作为string或viceversa? 就像当您将json值保存为整数时,您可以将其读取为字符串。 通过这种方式,您可以避免在需要显示int时将其解析为字符串,例如在TextView中,SharedReferences使用xml存储数据,xml文件的外观与此完全相同: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <int
通过这种方式,您可以避免在需要显示int时将其解析为字符串,例如在TextView中,SharedReferences使用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使用键
查找具有该值的xml节点,并使用节点类型根据请求验证该值。如果不匹配,则抛出ClassCastException
。
在JSON中,这种验证不存在,解析器将尝试解析该值,并抛出一个JSONException
“如果您使用的是org.JSON对象”
但是,如果您希望实现JSON行为并仍然轻松地存储数据,那么可以将其包装在JSONObject中,然后将其序列化为字符串并存储。当检索它时,再次将其解析为JSONObject,然后从中读取值。您可以编写一个帮助器类来为您执行此操作。例如:
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);
为什么不将整数转换为字符串,然后再转换回来呢?谢谢您的快速回复,但是这样保存为整数的值就永远不能被读取为字符串。这个问题的动机是我编写了一个应用程序,它可以从/到json读取和写入值,但现在我必须将其更改为使用SharedReferences。问题是,在编写应用程序时,我会根据需要读取值。现在,如果我没有找到解决方案,我必须在代码周围(数千行)进行更改,以便根据保存值的方式读取值。这是一个非常聪明的解决方案,我想我会使用这种方法。感谢您的帮助,我非常高兴,但这不是一个读取与书面形式不同的共享引用的解决方案