Android check SharedPreferences for value type
Asked Answered
N

3

6

I've got some key-value pairs in SharedPreferences, there are ints, floats, Strings, etc. Is there any way to check if a given key is of a specific type?

EDIT

I've studied the documentation and available methods. Sadly, it seems to me that i'd need to make it a "dirty" way, just trying every get method until i get value different than default set as parameter. this is the only one i figured out, but don't like it much...

Nebuchadnezzar answered 13/4, 2015 at 22:9 Comment(2)
Are you storing different data types in the same key at different points? Standard practice is to just define each key as a static global, and you shouldn't ever need to check what type it is, because each key should only ever have one type of data in it.Mercymerdith
@DanielNugent I know, the point is i try to define a method that will only get a key as parameter, and then do some job depending on the value type.Nebuchadnezzar
O
14

You can iterate through all the entries in SharedPreferences and check the data type of each entry by using getClass function of the value.

Map<String,?> keys = sharedPreferences.getAll();

for(Map.Entry<String,?> entry : keys.entrySet())
{
  Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
  Log.d("data type", entry.getValue().getClass().toString());

  if ( entry.getValue().getClass().equals(String.class))
    Log.d("data type", "String");
  else if ( entry.getValue().getClass().equals(Integer.class))
    Log.d("data type", "Integer");
  else if ( entry.getValue().getClass().equals(Boolean.class))
    Log.d("data type", "boolean");

}
Orthodontist answered 13/4, 2015 at 22:26 Comment(3)
Will try this one, looks like my idea, but is more elegantNebuchadnezzar
Works, thank you for a very nice answer. I can finally go to sleep, not worrying anymore ;)Nebuchadnezzar
Doesn't getAll() get the entries? Why you name the variable as keys?Rwanda
G
1

maybe late for the party, but a kotlin approach could be:

 fun readPreference(key: String) : Any? {
    val keys = sharedPrefs?.all
    if (keys != null) {
        for (entry in keys) {
            if (entry.key == key) {
                return entry.value
            }
        }
    }
    return null
}

where sharedPrefs is something previous initialized as:

sharedPrefs = this.applicationContext.getSharedPreferences("userdetails", Context.MODE_PRIVATE)
Gymkhana answered 6/3, 2018 at 17:10 Comment(0)
P
0

Actually if you check the documentation for SharedPreferences (http://developer.android.com/reference/android/content/SharedPreferences.html) you're not going to find anything related to your question so probably it can't be done. Optionally you can check for the existence of a particular key using contains method, but for fetching a key you need to specify the type using some of:

getBoolean(String key, boolean defValue);
getFloat(String key, float defValue);
getInt(String key, int defValue);
...
Pin answered 13/4, 2015 at 22:17 Comment(2)
I've studied the documentation and available methods. Sadly, it seems i'd need to make it a "dirty" way, just trying every get method until i get value different than default set as parameter. this is the only one i figured out, but don't like it much...Nebuchadnezzar
sounds like a dirty hack, maybe SharedPreferences is not the tool that you're looking for...Pin

© 2022 - 2024 — McMap. All rights reserved.