Android: Sort data retrieved from SharedPreferences

淺唱寂寞╮ 提交于 2021-01-29 18:29:33

问题


I am trying to store data in Android. I am using the SharedPreferences. And I am retrieving these data by using:

 SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
            Map<String, ?> keys = myPrefs.getAll();
            for (Map.Entry<String, ?> entry : keys.entrySet()) {
                Log.i("map values", entry.getKey());
                            //some code
 }

EDIT:

But data retrieved are not in the same order as they were inserted. How to get the same order?


回答1:


Copy the resulting Map into an implementation of SortedMap, e.g. TreeMap.

Like this (sort by key):

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
TreeMap<String, ?> keys = new TreeMap<String, Object>(myPrefs.getAll());
for (Map.Entry<String, ?> entry : keys.entrySet()) {
    Log.i("map values", entry.getKey());
    //some code
}

To sort by value & not lose any key-value pairs (since a Map will readily allow duplicate values mapping to different keys) you'd need to first convert it into a List and sort that.

    List<Pair<Object, String>> sortedByValue = new LinkedList<Pair<Object,String>>();
    for (Map.Entry<String, ?> entry : keys.entrySet()) {
        Pair<Object, String> e = new Pair<Object, String>(entry.getValue(), entry.getKey());
        sortedByValue.add(e);
    }

    // Pair doesn't have a comparator, so you're going to need to write one.
    Collections.sort(sortedByValue, new Comparator<Pair<Object, String>>() {
        public int compare(Pair<Object, String> lhs, Pair<Object, String> rhs) {
            // This is a naive and shitty comparator, but it works for
            // arbitrary objects. Sort of. Tweak depending on the order you need.
            String sls = String.valueOf(lhs.first);
            String srs = String.valueOf(rhs.first);
            int res = sls.compareTo(srs);
            // Sort on value first, key second
            return res == 0 ? lhs.second.compareTo(rhs.second) : res;
        }
    });

    for (Pair<Object, String> pair : sortedByValue) {
        Log.i("map values", pair.first + "/" + pair.second);
    }



回答2:


Add a property with the order, like so... First save the settings...

public static void saveSettings(final Editor editor, final String [] order) {
    final String csl = toString(order);//comma separated
    editor.putString("insert_order", csl);
    for (int i = 0; i < values.length; i++) {
        editor.putString(values[i], your_value[i]);
    }
}

Now load them:

public static List<String> loadSetting(final SharedPreferences preferences) {
    final List<String> inOrder = new ArrayList<>(); 
    final String[] ordering = preferences.getString("insert_order", "").split(",");
    for (final String item : ordering) {
        final String value = (String) preferences.getString(item, "");
        inOrder.add(value);
    }
    return inOrder;
}


来源:https://stackoverflow.com/questions/11207640/android-sort-data-retrieved-from-sharedpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!