[英]Misbehavior with sharedpreferences
Before you judge me, I'd like to say I read theese: 在您判断我之前,我想说一下我读过这些:
But I still can't understand, can't get things work. 但是我仍然不明白,无法使事情正常进行。 I get totally misbehavior of my preferences.
我完全不喜欢自己的喜好。 My code:
我的代码:
public static SharedPreferences sharedAppPreferences;
public static final String AppsListKey = "AppListKey";
public static final String AppsPreferences = "AppsPreferences";
public static ArrayList<String> packageNames;
public void chooseApps(View view) {
sharedAppPreferences = getSharedPreferences(AppsPreferences, Context.MODE_PRIVATE);
if (sharedAppPreferences.contains(AppsListKey)) {
Set<String> buffer = new LinkedHashSet<String>(sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>()));
packageNames = new ArrayList<String>(buffer);
} else {
packageNames = new ArrayList<String>();
}
PackageManager packageManager = getPackageManager();
int flags = PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES | PackageManager.GET_UNINSTALLED_PACKAGES;
List<ApplicationInfo> packageList = packageManager.getInstalledApplications(flags);
for (ApplicationInfo pack : packageList) {
if (((pack.flags & ApplicationInfo.FLAG_SYSTEM) == 1) || packageNames.contains(pack.loadLabel(packageManager).toString())) {
// System application or already in array
} else {
// Installed by user and isnt in array
packageNames.add(pack.loadLabel(packageManager).toString());
}
}
Editor editor = sharedAppPreferences.edit();
Set<String> buffer1 = new LinkedHashSet<String> (packageNames);
editor.putStringSet(AppsListKey, buffer1);
editor.commit();
//packageNames.clear();
//buffer1.clear();
buffer1 = new LinkedHashSet<String>(sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>()));
packageNames = new ArrayList<String>(buffer1);
AppList appList = new AppList();
appList.show(getSupportFragmentManager(), "AppList");
}
Why first time I run my app I get list like 为什么第一次运行我的应用程序时会显示如下列表
[Skype, Facebook, Whatsapp, Twitter, Google+]
It's ok as long as app is running... But if I kill my app and restart I get now totally different list like 只要应用程序正在运行就可以...但是如果我杀死我的应用程序并重新启动,我会得到完全不同的列表,例如
[Whatsapp, Google+, Skype, Twitter, Facebook]
Could someone explain me please what is here wrong? 有人可以向我解释一下这是怎么回事吗?
The only difference in your list before and after, is the ordering... 列表前后的唯一区别是订购...
To expand on what I have been talking about, I just realised your mistake.. 为了扩展我一直在谈论的内容,我刚刚意识到您的错误。
LinkedHashSet
is ordered. LinkedHashSet
是有序的。 However, you are storing in 但是,您存储在
Set<String> buffer
Set<String>
is not ordered... Set<String>
不排序...
So it jumbles it up again. 因此,它再次变得混乱。
You need to store it in parameter of LinkedHashSet
like below 您需要将其存储在
LinkedHashSet
参数中,如下所示
LinkedHashSet<String> buffer = new LinkedHashSet<String>(sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>()));
(There are two occasions i see this needs to be changed. ) (我有两种情况需要对此进行更改。)
Edit: 编辑:
One final thing you can do to help, is rather than create a new list, cast the existing one from shared prefs ... 您可以做的最后一件事是,而不是创建新列表,而是从共享首选项中投射现有列表...
LinkedHashSet<String> buffer = (LinkedHashSet<String>)sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.