Android共享首选项 - 类的数组列表

时间:2016-04-13 13:25:27

标签: java android class arraylist sharedpreferences

我有一个Player类,它有一个名字和一个分数的arraylist。使用按钮动态添加分数。我想使用共享首选项来保存我在主要活动中创建的Arraylist<Player> players。我尝试了几种解决方案但没有成功。

到目前为止我尝试了什么:

链接: Save ArrayList to SharedPreferences

当我使用ObjectSerializer类时,我无法导入包org.apache.pig.impl.util;这会导致出现错误,例如:

enter image description here

因为WrappedIOException在该包中。

Tinydb: 微小的数据库似乎是一个很好的解决方案但我在检索数据时遇到错误。这些是我从java文件中更改的方法:

    public void putListObject(String key, ArrayList<Player> playerArray){
        checkForNullKey(key);
        Gson gson = new Gson();
        ArrayList<String> playerStrings = new ArrayList<String>();
        for(Player player : playerArray){
            playerStrings.add(gson.toJson(player));
        }
        putListString(key, playerStrings);
    }

    public ArrayList<Player> getListObject(String key, Class<?> mClass) {
        Gson gson = new Gson();

        ArrayList<String> objStrings = getListString(key);
        ArrayList<Player> objects = new ArrayList<Player>();

        for (String jObjString : objStrings) {
            Object value = gson.fromJson(jObjString, mClass);
            objects.add((Player) value);
        }
        return objects;
    }

public void putListObject(String key, ArrayList<Player> playerArray){
    checkForNullKey(key);
    Gson gson = new Gson();
    ArrayList<String> playerStrings = new ArrayList<String>();
    for(Player player : playerArray){
        playerStrings.add(gson.toJson(player));
    }
    putListString(key, playerStrings);
}

玩家等级:

public class Player implements Serializable {
    private String name;
    private ArrayList<Integer> scores = new ArrayList<>();

    public Player(String name){
        this.name = name;
    }


    public ArrayList<Integer> getScores() {
        return scores;
    }

    public void setScore(int score) {
        scores.add(score);
    }

如果有人对如何存储和检索我的播放器arraylist有一个很好的解决方案,请分享

2 个答案:

答案 0 :(得分:1)

尝试将对象转换为JSON,然后再保存到“共享首选项”。 GSON是一款易于使用的快速工具。

答案 1 :(得分:0)

我的一个项目中有类似的要求,我可以在GSON的帮助下完成。请查看以下示例代码。

   /**
     * Remove preference value.
     *
     * @param mContext The context to use.  Usually your {@link android.app.Application}
     *                 or {@link Activity} object.
     * @param key      The name of the preference to put.
     * <br><br>
     * @return Returns true if preference value were successfully removed from
     * persistent storage.
     */
    public static <E>boolean putList(Context mContext, String key, List<E> objectList){
        return SharedPreferenceUtils.putString(mContext, key, new Gson().toJson(objectList));
    }

    /**
     * Remove preference value.
     *
     * @param mContext The context to use.  Usually your {@link android.app.Application}
     *                 or {@link Activity} object.
     * @param key          The name of the preference.
     * <br><br>
     * @return Returns true if preference value were successfully removed from
     * persistent storage.
     */
    public static <E>List<E> getList(Context mContext, String key){
        TypeToken<List<E>> token = new TypeToken<List<E>>() {};
        String json = SharedPreferenceUtils.getString(mContext, key, null);
        List<E> objectList = new Gson().fromJson(json, token.getType());
        return objectList;
    }

示例:

List<Player> list = new ArrayList<Player>();
list.add(player);
putList(this, "j", list);//Store List to SharedPreference

list = getList(this, "j");//Get List from SharedPreference
Log.e("DATA", list.toString());

有关SharedPreferenceUtils的完整源代码,请查看此link