How to store and retrieve an object from Gson in android? [duplicate]

人走茶凉 提交于 2019-12-11 05:42:17

问题


In my activity, the user has an array of images he picks, and then I want to store them in preferences. Then the user leaves the activity, and comes back, I want those images still loaded into an ImageView. I'm trying to use Gson to accomplish this, but am having trouble and can't see what I'm doing wrong. Hoping external insight may help me with this obvious answer I'm just not seeing.

Thank you.

Here is my code so far.

private void savePreferencesForImages(String key, ArrayList<Image> imageList)
    {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(imageList);
        editor.putString(key,json);
        editor.commit();
    }


//Storing Local data.
private void loadPreferences()
{
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    Gson gson = new Gson();
    String gsonString = sharedPreferences.getString("userImages", "");
    ArrayList<Image> images = gson.fromJson(gsonString, Image.class);//Ask StackOverflow tomorrow.
}

回答1:


In the part of retrieving the data you will need a type that deserialize the String into a List of Images...

like:

private void loadPreferences()
{
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    Gson gson = new Gson();
    Type type = new TypeToken<List<Image>>() {}.getType();
    String gsonString = sharedPreferences.getString("userImages", "");
    List<Image> images = gson.fromJson(gsonString, type);
}



回答2:


One thing you can do is save the images individually and then load them into an ArrayList upon retrieval.

private void savePreferencesForImages(ArrayList<Image> imageList)
{
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson = new Gson();
    int imgKey = 0;
    for (Image img: imageList) {
        String json = gson.toJson(imageList);
        editor.putString("" + imgKey,json);
        editor.commit();
        imgKey++;
    }
}


private ArrayList<Image> loadPreferences()
{
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    Gson gson = new Gson();
    Map<String, ?> allPrefs = sharedPreferences.getAll();
    String gsonString = sharedPreferences.getString("userImages", "");
    ArrayList<Image> images = new ArrayList<Image>();
    if (!allPrefs.isEmpty()) {
        for (Map.Entry<String, ?> entry : allPrefs.entrySet()) {
            String json = entry.getValue().toString();
            Image temp = gson.fromJson(json, Image.class);
            images.add(temp);
        }
    }
    return images;
}

This is potentially the most over-complicated way to go about this but it is the first thing I could think of. You could also just make your own ImageManager class or something and emulate the behavior of an ArrayList, with a getImage(key) method and so on. This would eliminate the complications that are brought about when trying to save an ArrayList.



来源:https://stackoverflow.com/questions/37927113/how-to-store-and-retrieve-an-object-from-gson-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!