Mr_en 2016-01-06 02:06 采纳率: 10%
浏览 1335
已采纳

关于android SharedPreference的问题,在线等!谢谢!!!

SharedPreferences 有没有办法根据vlaue获取对应的key值,除了循环SharePreferences里面的内容。

  • 写回答

4条回答 默认 最新

  • tony4geek 2016-01-06 02:14
    关注

    You should note that while keys are guaranteed to be unique in SharedPreferences, there's no guarantee that the values will be unique. As such, this function will only return the key for the first matching value.

     String findKey(SharedPreferences sharedPreferences, String value) {
        for (Map.Entry<String, ?> entry: sharedPreferences.getAll()) {
            if (value.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return null; // not found
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?