Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:48:31+00:00 2026-06-12T08:48:31+00:00

Is it possible to get the key value from a SharedPreferences file if I

  • 0

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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:48:33+00:00Added an answer on June 12, 2026 at 8:48 am

    You can use the SharedPreferences.getAll method.

    String findKey(SharedPreferences sharedPreferences, String value) {
        for (Map.Entry<String, ?> entry: sharedPreferences.getAll().entrySet()) {
            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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java Hashmap: How to get key from value? I know that a
I know that NSUserDefaults can read the key and value from plist file. Is
Possible Duplicate: best way to get the key of a key/value javascript object foo
Possible Duplicate: PHP get both array value and array key I am using Codeigniters'
Possible Duplicate: Get file name from URI string in C# How to extract file
how can I get value from key while the element in for each is
is it possible to get entity key from EntityReference property without loading it? I
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic According
does anyone know how to (if possible) get the owner of the tag on
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.