无法从HashMap中检索Double <integer,double =“”> </integer,>

时间:2015-03-16 20:02:34

标签: java android hashmap gson

不知何故,我无法从使用Gson制作的HashMap中检索Double。

Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder()
            .create().fromJson(json, Map.class);
Integer ifilmId = filmId;
Double rating = ratingMap.get(ifilmId);

在这段代码中,我已经验证了ratingMap包含{2 = 5.0},但是当我ratingMap.get(ifilmId)(我已经验证ifilmId实际上是2)时,变量rating一片空白。我在这里错过了什么吗?

我按以下方式创建HashMap:

if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Integer>();
        ratingMap.put(filmId, rating);

} else {
        ratingMap = (Map<Integer, Integer>) new GsonBuilder().create()
                .fromJson(json, Map.class);
        ratingMap.put(Integer.valueOf(filmId), rating);
}

我让Gson将Integer格式化为Double,这似乎工作正常但我无法检索它。

总代码,包括保存到Androids SharedPreferences

public void saveRating(int rating, int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    Map<Integer, Integer> ratingMap;
    if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Integer>();
        ratingMap.put(filmId, rating);

    } else {
        ratingMap = (Map<Integer, Integer>) new GsonBuilder().create()
                .fromJson(json, Map.class);
        ratingMap.put(Integer.valueOf(filmId), rating);
    }

    json = new GsonBuilder().create().toJson(ratingMap, Map.class);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(LOCAL_MAP_RATING_KEY, json);
    editor.commit();
}

/*
 * returns null if no rating found
 */
public Map<Integer, Integer> getRatingFor(int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    if (json.equals("")) {
        return null;
    }

    Map<Integer, Integer> ratingMap = (Map<Integer, Integer>) new GsonBuilder()
            .create().fromJson(json, Map.class);
    Log.d("map", ratingMap.toString());
    Integer ifilmId = filmId;
    Integer rating = ratingMap.get(ifilmId);
    if(rating == null) { //because of this we have to prevent a 0 rating
        return null;
    } else {
        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        returnMap.put(filmId, rating.intValue());
        return returnMap;
    }
}

2 个答案:

答案 0 :(得分:0)

确保在保存

时不传递空变量
saveRating(int rating, int filmId){
    Log.d(TAG, String.valueOf(rating));
}

if (json.equals("")) {
    // noting ever saved
    ratingMap = new HashMap<Integer, Double>();    <--- Double not Integer
    ratingMap.put(filmId, 5.0);

} else {
    ratingMap = (Map<Integer, Double>) new GsonBuilder().create()
            .fromJson(json, Map.class);           <--- double not Integer
    ratingMap.put(Integer.valueOf(filmId), 5.0);
}

确保使用双打时间 使用5.0
不是5

答案 1 :(得分:-1)

public void saveRating(Double rating, int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    Map<Integer, Double> ratingMap;
    if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Double>();
    } else {
        ratingMap = (Map<Integer, Double>) new GsonBuilder().create().fromJson(json, Map.class);
    }
    ratingMap.put(filmId, rating);
    ratingMap.put(3, 5.0d); // JUST FOR TEST
    json = new GsonBuilder().create().toJson(ratingMap, Map.class);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putString(LOCAL_MAP_RATING_KEY, json);
    editor.commit();
}

/*
 * returns null if no rating found
 */
public Map<Integer, Double> getRatingFor(int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");

    if (json.equals("")) {
        return null;
    }

    Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder().create().fromJson(json, Map.class);
    Log.d("map", ratingMap.toString());
    Log.d("map", ratingMap.get(3) + ""); // JUST FOR TEST
    Integer ifilmId = filmId;
    Double rating = ratingMap.get(ifilmId);

    if (rating == null) { //because of this we have to prevent a 0 rating
        return null;
    } else {
        Map<Integer, Double> returnMap = new HashMap<Integer, Double>();
        returnMap.put(filmId, rating);
        return returnMap;
    }
}