0

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

4

2 回答 2

0

SharedPreferences uses xml to store data and the xml file looks exactely like this:

<?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>

So android uses the key to find the xml node that has the value and uses the node type to verify the value against the request. and throws ClassCastException if they didn't match. in JSON such verification doesn't exist, the parser would try to parse the value and throws a JSONException 'if you are using org.json objects'.

but if you want to achieve JSON behavior and still store your data easily, you can wrap it in a JSONObject then serialize it to String and store it. and when retrieve it parse it as JSONObject again and then read the values from it.

于 2013-03-17T14:40:22.230 回答
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);
于 2013-03-17T14:46:05.087 回答