我可以从共享preferences的关键,如果知道相关的价值?关键、价值、preferences

2023-09-07 15:41:22 作者:似水往昔浮流年

是否有可能从一个共享preferences的钥匙的价值,如果我知道的价值的价值它是搭配文件?

比方说,我已经去到共享preferences文件,并通过用户操作,已经得到了价值,英国伦敦。

现在我想要得到的价值,英国伦敦,然后使用我的下一个步骤相关的KEY,这可能吗?

在code我目前使用从SharedPerferences GETALL数据,排序是按值,填充一个AlertDialog这个数据处理选择从AlertDialog一个选项,一个用户,然后返回这个选择的价值。我现在需要的多数民众赞成搭配价值的关键。

 公共无效openServerDialog(){


        最后一个共享preferences我的preFS = this.getShared preferences(文件名,MODE_PRIVATE);
        TreeMap的<字符串,>键=新TreeMap的<字符串,对象>(我的prefs.getAll());
        为(?Map.Entry的<字符串,>输入:keys.entrySet()){
            Log.i(映射值,entry.getKey());
            //一些code
        }


      名单<对<对象,字符串>> sortedByValue =新的LinkedList<对<对象,字符串>>();
        为(?Map.Entry的<字符串,>输入:keys.entrySet()){
            对<对象,字符串> E =一双新<对象,字符串>(entry.getValue(),entry.getKey());
            sortedByValue.add(E);
        }

     //对没有比较,所以你将需要写一个。
        Collections.sort(sortedByValue,新的比较<对<对象,字符串>>(){
            公众诠释比较(对<对象,字符串> LHS,对<对象,字符串>右){

                字符串SLS =将String.valueOf(lhs.first);
                字符串SRS =将String.valueOf(rhs.first);
                INT RES = sls.compareTo(SRS);
                //排序价值第一,第二的关键
                返回水库== 0? lhs.second.compareTo(rhs.second):资源;
            }
        });

        对于(对<对象,字符串>对:sortedByValue){
            Log.i(映射值,pair.first +/+ pair.second);
        }


      收藏<> stringArrayList = keys.values​​();
      最后的CharSequence [] prefsCharSequence = stringArrayList.toArray(新的CharSequence [stringArrayList.size());

        新AlertDialog.Builder(本)
        .setTitle(R.string.server_title)
        .setItems(prefsCharSequence,
        新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface dialoginterface,int i)以{

                setServer(prefsCharSequence [I]);

            }
        })
        。显示();
    }
 

解决方案 干货分享 Eclipse 的使用配置整理

您可以使用共享preferences.getAll 方法。

 字符串findKey(共享preferences共享preferences,字符串值){
    对于(Map.Entry的<字符串,>进入:共享preferences.getAll()){
        如果(value.equals(entry.getValue())){
            返回entry.getKey();
        }
    }
    返回null; // 未找到
}
 

您应该注意,虽然密钥保证在共享preferences独一无二的,但也不能保证这些值是唯一的。因此,该功能将只返回键,第一个匹配的值。

Is it possible to get the "key" value from a SharedPreferences file if I know the "value" value it is paired with?

Let's say I've gone into the SharedPreferences file and, through a user action, have got the value "London, UK".

Now I want to get the KEY associated with the value "London, UK" and then use that for my next step, is this possible?

The code I use at the moment to getAll data from the SharedPerferences, sort it by value, populate an AlertDialog with this data, deal with a user choosing an option from the AlertDialog and then returning the value of that choice. I now need the KEY that's paired with the value.

public void openServerDialog() {


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


      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) {

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


      Collection<?> stringArrayList = keys.values();
      final CharSequence[] prefsCharSequence = stringArrayList.toArray(new CharSequence[stringArrayList.size()]);

        new AlertDialog.Builder(this)   
        .setTitle(R.string.server_title)
        .setItems(prefsCharSequence, 
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialoginterface, int i) {

                setServer(prefsCharSequence[i]);   

            }
        })
        .show();
    }

解决方案

You can use the SharedPreferences.getAll method.

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
}

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.