Android从SharedPrefs获取设置正在破坏订单

时间:2016-01-03 18:13:18

标签: android list set sharedpreferences

我有这样的方法:

public static void setSearchHistory(Activity activity, int channelId) {
    SharedPreferences sp = getSharedPreferences(activity);
    SharedPreferences.Editor prefsEditor = sp.edit();

    Set<String> searchHistory = sp.getStringSet("searchHistory", new LinkedHashSet<String>());
    List<String> shList = new ArrayList<>();
    shList.addAll(searchHistory);

    if(shList.size() > 0) {
        boolean wasAdded = false;
        for(int i=0;i<shList.size();i++) {
            Integer channelID = Integer.parseInt(shList.get(i));
            if(channelId == channelID) {
                shList.remove(i);
                shList.add(0, String.valueOf(channelId));
                wasAdded = true;
                break;
            }
        }
        if(!wasAdded) {
            shList.add(0, String.valueOf(channelId));
        }
    } else {
        shList.add(String.valueOf(channelId));
    }

    if(shList.size() > 10) {
        for(int i=10;i<shList.size();i++) {
            shList.remove(i);
        }
    }

    searchHistory = new LinkedHashSet<>(shList);

    prefsEditor.putStringSet("searchHistory", searchHistory);
    prefsEditor.apply();
}

此方法会获得channelId参数并将其添加到Set的第一个位置并保存回SharedPreferences。它还控制列表中是否已channelId。如果是,则从当前位置移除channelId并将其添加到第一个位置。然后删除最后一项,直到其大小为10.

我将List放回Set,列表顺序正确。我还为他们截了屏幕截图:

添加Set之后的Listenter image description here

但是当我从Set获得SharedPreferences时,如下所示,列表的顺序非常随机地混淆。

SharedPreferences sp = getSharedPreferences(activity);

Set<String> searchHistory = sp.getStringSet("searchHistory", new LinkedHashSet<String>());
List<String> shList = new ArrayList<>();
shList.addAll(searchHistory);

保存到Set后的SharedPreferencesenter image description here

这真的很烦人,我不希望它混淆订单。 :(

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

我从另一个问题找到了解决方法:here

我完全删除了Set个对象,并将其替换为JSONArray,将其保存到SharedPrefs String,如下所示;

List放入SharedPrefs:

public static void setSearchHistory(Activity activity, int channelId) {
    try {
        SharedPreferences sp = getSharedPreferences(activity);
        SharedPreferences.Editor prefsEditor = sp.edit();

        JSONArray searchHistory;
        List<String> shList = new ArrayList<>();
        if(sp.getString("searchHistory", null) != null) {
            searchHistory = new JSONArray(sp.getString("searchHistory", null));
            for(int i=0;i<searchHistory.length();i++) {
                shList.add(searchHistory.get(i).toString());
            }
        }

        if(shList.size() > 0) {
            boolean wasAdded = false;
            for(int i=0;i<shList.size();i++) {
                Integer channelID = Integer.parseInt(shList.get(i));
                if(channelId == channelID) {
                    shList.remove(i);
                    shList.add(0, String.valueOf(channelId));
                    wasAdded = true;
                    break;
                }
            }
            if(!wasAdded) {
                shList.add(0, String.valueOf(channelId));
            }
        } else {
            shList.add(String.valueOf(channelId));
        }

        if(shList.size() > 10) {
            for(int i=10;i<shList.size();i++) {
                shList.remove(i);
            }
        }

        searchHistory = new JSONArray(shList);

        prefsEditor.putString("searchHistory", searchHistory.toString());
        prefsEditor.apply();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

从SharedPrefs获取List

public static List<Channel> getSearchHistory(Activity activity) {
    try {
        SharedPreferences sp = getSharedPreferences(activity);

        List<String> shList = new ArrayList<>();
        if(sp.getString("searchHistory", null) != null) {
            JSONArray searchHistory = new JSONArray(sp.getString("searchHistory", null));
            for(int i=0;i<searchHistory.length();i++) {
                shList.add(searchHistory.get(i).toString());
            }
        }

        List<Channel> channelList = new ArrayList<>();

        for(int i=0;i<shList.size();i++) {
            Channel channel = Channel.findChannelByID(Integer.parseInt(shList.get(i)), activity);
            channelList.add(channel);
        }

        return channelList;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

像魅力一样工作。耶!