jau*_*ard 19 android save sharedpreferences
我尝试保存/恢复一组字符串,除了一件事以外一切正常.当我创建我的字符串时,我把:
Set<String> set = new HashSet<String>();
for(int i=0; i<toggles.size();i++){
set.add(toggles.get(i).serialise());
}
Run Code Online (Sandbox Code Playgroud)
订单例如是"blutooth""application""data".当我回来时:
Set<String> set = prefs.getStringSet(key, new HashSet<String>());
for (String toggle : set){
Toggle t = new Toggle();
t.deserialize(toggle);
toggles.add(t);
}
Run Code Online (Sandbox Code Playgroud)
我得到"应用程序""蓝牙""数据"他们按名称排序,我不想要这个.我希望获得相同的订单.有人可以帮帮我吗?
Com*_*are 20
这是不可能的.集合是无序集合.
您可以将字符串列表保存为带有分隔符的单个字符串。例如,这是您的数组:
["Italy", "France", "Spain", "Japan", "United States"]
Run Code Online (Sandbox Code Playgroud)
您可以将其另存为:
"Italy;France;Spain;Japan;United States"
Run Code Online (Sandbox Code Playgroud)
在代码中:
SharedPreferences sharedPref = mContext.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
// countries is your List
String countriesString = TextUtils.join(";", countries);
editor.putString("countries", countriesString);
editor.apply();
Run Code Online (Sandbox Code Playgroud)
要检索您的数据:
final String countriesString = sharedPref.getString("countries", "");
List<String> countries = new ArrayList<>();
if (!countriesString.isEmpty()){
countries = new ArrayList<>(Arrays.asList(countriesString.split(";")));
}
Run Code Online (Sandbox Code Playgroud)
您可以通过数字前缀你的字符串,例如00application,01bluetooth,02data,在为了你想出来.把Set<String>从返回getStringSet在Array<Set>和排序.
Set<String> set = prefs.getStringSet(key, new HashSet<String>());
Array<String> a = set.toArray();
java.util.Arrays.sort(a);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10692 次 |
| 最近记录: |