0

I'm saving a vector to sharedpreferences using Gson

This is my setters and getters but I seem to have some warning on my getter.

My setter doesn't have any warnings

     Gson gson = new Gson();
     String json = gson.toJson(al);
     editor.putString("accountLabels", json);
     editor.commit();

My getter warns me "Type safety: The expression of type Vector needs unchecked conversion to conform to Vector"

    Gson gson = new Gson();
    String json = myPref.getString("accountLabels", "Error");
    Vector<AccountLabels> obj = gson.fromJson(json, Vector.class);
    return obj;

I don't know how much of a work around it is to save objects or even vector of objects in sharedpreferences, but it seems like the best solution for me.

Mike
  • 826
  • 11
  • 31

1 Answers1

1

I'm not sure what the question is. I'm guessing it's to do with resolving the unchecked conversion warning. To do that, change

Vector<AccountLabels> obj = gson.fromJson(json, Vector.class);

to

Vector<AccountLabels> obj = gson.fromJson(json, new TypeToken<Vector<AccountLabels>>(){}.getType());

The warning is occurring because the fromJson method is returning type Vector, but it's being assigned to a reference of type Vector<AccountLabels>.

(Note: Since 2001 (I think) and the newer Collections API, use of Vector is frowned upon.)

Community
  • 1
  • 1
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • Yes it is about the warning, thanks for the answer it helps. Also that's good to know. It's kind of disappointing as well as Vectors are so portable. – Mike Apr 01 '13 at 04:40