google gson LinkedTreeMap class cast to myclass

后端 未结 5 826
时光取名叫无心
时光取名叫无心 2020-11-29 05:01

I knew this question has been asked before. Due to my novice skill in java and android. I can\'t resolve this issue for more than a week.

One of my friend and i dev

5条回答
  •  野性不改
    2020-11-29 05:46

    In my case error occurred while fetching a list of objects from shared preferences. The error was solved by adding TypeToken as shown below:

     public static  GenericClass getListOfObjectsFromSharedPref(Context context,String preferenceFileName, String preferenceKey ){
    
        SharedPreferences sharedPreferences = 
        context.getSharedPreferences(preferenceFileName, 0);
        Type type = new TypeToken>() {}.getType();
        String json = sharedPreferences.getString(preferenceKey, "");
        final Gson gson = new Gson();
        return gson.fromJson(json, type);
    }
    

    ....

提交回复
热议问题