您好,我正在尝试获取保存在首选项文件中的整数数组。
int[] ints = {2, 3, 4};
Hashtable<String, int[]> hashTable = new Hashtable<String, int[]>();
hashTable.put("test", ints);
pref.getPref().put(hashTable);
pref.getPref().flush();
Gdx.app.log(String.valueOf(pref.getPref().get()), "");
但是我保存了 0 个首选项。我也尝试过使用 HashMap。
最佳答案
您无法将数组对象放入首选项中,但是您可以使用字符串来完成此操作,因此您所需要做的就是在保存之前进行序列化,并在从首选项中获取值后进行反序列化。
Libgdx 通过提供 JSON 支持序列化类(class)。您应该遵循的是:
Hashtable<String, String> hashTable = new Hashtable<String, String>();
Json json = new Json();
hashTable.put("test", json.toJson(ints) ); //here you are serializing the array
... //putting the map into preferences
String serializedInts = Gdx.app.getPreferences("preferences").getString("test");
int[] deserializedInts = json.fromJson(int[].class, serializedInts); //you need to pass the class type - be aware of it!
要了解有关 json 格式的更多信息,请访问 official json webpage
关于java - 如何将数组添加到首选项 (libGdx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32998712/