我正在使用Gson
将向量保存到共享偏好这是我的主持人和吸气者,但我似乎对我的吸气器有一些警告。
我的二传手没有任何警告
Gson gson = new Gson();
String json = gson.toJson(al);
editor.putString("accountLabels", json);
editor.commit();
我的getter警告我“类型安全:Vector类型的表达式需要未经检查的转换以符合Vector”
Gson gson = new Gson();
String json = myPref.getString("accountLabels", "Error");
Vector<AccountLabels> obj = gson.fromJson(json, Vector.class);
return obj;
我不知道有多少工作是在共享偏好中保存对象甚至是对象的矢量,但对我来说这似乎是最好的解决方案。
答案 0 :(得分:1)
我不确定问题是什么。我猜它与解决未经检查的转换警告有关。为此,请更改
Vector<AccountLabels> obj = gson.fromJson(json, Vector.class);
到
Vector<AccountLabels> obj = gson.fromJson(json, new TypeToken<Vector<AccountLabels>>(){}.getType());
警告正在发生,因为fromJson
方法返回的类型为Vector
,但它被分配给Vector<AccountLabels>
类型的引用。
(注意:自2001年以来(我认为)和更新的Collections API,use of Vector
is frowned upon。)