1

In the code below I have a method that add new key values into the file "contact". My question is if it's possible to create a method that search for a special value by some kind of loop of all keys inside the file "contact" and see if one of the values is equal to a special value? Preciate some help! Thanks!

String filename = "contacts";
SharedPreferences someInfo = getSharedPreferences(filename, 0);

public void addSharedPref(String key, String value){

SharedPreferences.Editor editor = someInfo.edit();
editor.putString(key, value);
editor.commit();
}
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159

3 Answers3

4

Use SharePreference.getAll() to return a Map, then use one of the methods of Map to search for a special value.

However, this isn't what SharedPreference is designed to do. There may be a better way of doing what you're trying to do. My motto in any work is "Don't use a screwdriver to drive in a nail."

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • OK, but what would be a better way? This is a task I'm doing and this is what the teacher recomended! The task is to develop a app that tag a selected image form the SD-card with the ID of a contact. And a good way should be to use the image filepath as the key and the contacts ID as the value. And the reason for why I want to search my key values is to check if a key value of an image filepath and contact ID already is inside the "contact" file or to be able to read all the key values into a listview. What about that? Is this stupid? Preciate alternatives! – 3D-kreativ Feb 25 '13 at 21:28
  • And the next level in the task is to be able to add several ID of contacts to a keys image filepath by adding the ID the the already existing value by a comma separation, like this "key", "value1, value2, value3". I guess it's possible but complicated to read all this values of a certain key bu spittling them!? All help is preciated! I'm trying to make it as simple as possible. Thanks in advance! :) – 3D-kreativ Feb 25 '13 at 21:32
2

to retrieve all values in the shared preference:

    SharedPreferences sp= context.getSharedPreferences("contacts", Context.MODE_PRIVATE);
    Map<String,?> keys = sp.getAll();
    for(Map.Entry<String,?> entry : keys.entrySet()){
        Log.d("map values",entry.getKey() + ": " + entry.getValue().toString()); 
}
zoya ali
  • 400
  • 2
  • 4
  • 15
0

As SharedPreference is a dictionary, there's no way to access it using the built-in stuff like a list. You have to have an indexer (name in the name-value-pair) in order to access stuff.

What you could do is directly access the SharedPreference file and find it that way.

I would suggest just attempting to access the SharedPreference you're looking for, and add error handling around it to use a default.

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • OK, but it would be esaier to understand if you could add a code example? – 3D-kreativ Feb 25 '13 at 20:57
  • A code example for what? You can find the file manually like this http://stackoverflow.com/questions/2566430/sharedpreferences-file – Codeman Feb 25 '13 at 21:00
  • OK, but I was wondering how I can read each key value to see if one of the key value is equal to a special value? Isn't that possible? – 3D-kreativ Feb 25 '13 at 21:05
  • I suggest you use JoeMalin's suggestion, it's much better than mine :) – Codeman Feb 25 '13 at 21:31