2

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?

Community
  • 1
  • 1
  • To follow up what i said, since I hadnt heard of `LinkedHashSet` it is an ordered version of `HashSet` so something else is jumbling it a long the way. (My apps run on API 10 and above, which doesnt support this) – IAmGroot May 21 '14 at 13:01
  • http://stackoverflow.com/questions/13446367/android-sharedpreferences-putstringset-order-sort does it mean, that Im tryin to do impossible thing? –  May 21 '14 at 13:07
  • They are using HashSets, before LinkHashSet existed or was in use on android (API 11). http://stackoverflow.com/questions/5080612/hashset-vs-linkedhashset What you are doing is **possible**. I just looked closer at your code and saw the issue :) – IAmGroot May 21 '14 at 13:36
  • No, it doesnt work, I guess because what getStringSet returns isnt LinkedHashSet, its simple HashSet. Am I wrong? –  May 21 '14 at 13:51

1 Answers1

3

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. However, you are storing in

Set<String> buffer 

Set<String> is not ordered...

So it jumbles it up again.

You need to store it in parameter of LinkedHashSet like below

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>());

There are alternatives to HashSets here

Community
  • 1
  • 1
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • I need to store/retrieve my list of apps using normal ArrayList? Did I understand you correctly? If so, how is it possible if PutStringSet and getStringSet takes 2 arguments (key, Set ) ? Or I'm totally lost ? –  May 21 '14 at 12:34
  • I did but it still doesnt work. packageNames must stay ArrayList for DialogFragment listview.. –  May 21 '14 at 13:56
  • @onskulis Sorry for slow response, busy working. Try my final edit. Else see the other alternatives. Hopefully that edit would fix it. – IAmGroot May 21 '14 at 14:30
  • thanks for help but seems its not going to work in this way. I achievied it using JSONArray. –  May 22 '14 at 06:04