【发布时间】:2013-01-23 12:13:34
【问题描述】:
我正在使用 SharedPreferences 来保存和加载我的 ArrayList,如下所示: (保存)
SharedPreferences loadGlobalVariables = getSharedPreferences(APP_NAME, 0);
Categories = new ArrayList<String>(loadGlobalVariables.getStringSet("Categories", new HashSet<String>()));
(加载) 和这个 (它们都工作正常,保存和加载都正确)
SharedPreferences saveGlobalVariables = getSharedPreferences(APP_NAME, 0);
SharedPreferences.Editor editor = saveGlobalVariables.edit();
editor.putStringSet("Categories", new HashSet<String>(Categories));
editor.commit();
但检索到的 ArrayList 的元素顺序与以前不同。我知道这一点是因为我将这个 ArrayList 作为一个列表放入一个 Dialog(每次打开它都会刷新这个列表。),按 Category.toArray(temArray),并且该列表不再按字母顺序排列。
之前,这个ArrayList里面的String元素是按字母顺序排序的。当我从 SharedPreferences 中检索它时,它不再按字母顺序排序。
提前感谢您的帮助。
【问题讨论】:
-
哈希和集合是无序的。
标签: android