12

I am developing a dictionary app. In my app, I assume that user wants to save favourite words. I have decided to use SharedPreferences to save these values (I am aware that SQLite and files are better but I am stuck to "SharedPreferences", so keep on with it).

Here below is my code:

@Override
public void onClick(View v) {                                       
    SharedPreferences faves = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    { 
        SharedPreferences.Editor editor = faves.edit();
        editor.putString("favourite", mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ",");
        editor.commit();    
    }
    Log.i(CONTENT_TAG,"Favourite saved!");

    Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
    toast.show();   
}

The problem is that it does not retain more than one favourite word. I mean only one favourite word is saved and when a new one is added, the previous is erased.

So, how can the above code be edited so that this problem is solved?

Can you guys there help? Thank you very much.

Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
Niamh Doyle
  • 1,909
  • 7
  • 30
  • 42

6 Answers6

20

You can save multiple favorites in a single preference by adding numerous favorites in a single string, each favorite item separated by comma. Then you can use convertStringToArray method to convert it into String Array. Here is the full source code.
Use MyUtility Methods to save multiple favorite items.

            MyUtility.addFavoriteItem(this, "Sports");
            MyUtility.addFavoriteItem(this, "Entertainment");

get String array of all favorites saved

String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"};

Save these methods in separate Utility class

 public abstract class MyUtility {

    public static boolean addFavoriteItem(Activity activity,String favoriteItem){
        //Get previous favorite items
        String favoriteList = getStringFromPreferences(activity,null,"favorites");
        // Append new Favorite item
        if(favoriteList!=null){
            favoriteList = favoriteList+","+favoriteItem;
        }else{
            favoriteList = favoriteItem;
        }
        // Save in Shared Preferences
        return putStringInPreferences(activity,favoriteList,"favorites");
    }
    public static String[] getFavoriteList(Activity activity){
        String favoriteList = getStringFromPreferences(activity,null,"favorites");
        return convertStringToArray(favoriteList);
    }
    private static boolean putStringInPreferences(Activity activity,String nick,String key){
        SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, nick);
        editor.commit();                        
        return true;        
    }
    private static String getStringFromPreferences(Activity activity,String defaultValue,String key){
        SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
        String temp = sharedPreferences.getString(key, defaultValue);
        return temp;        
    }

    private static String[] convertStringToArray(String str){
        String[] arr = str.split(",");
        return arr;
    }
}

If you have to add extra favorites. Then get favorite string from SharedPreference and append comma+favorite item and save it back into SharedPreference.
* You can use any other string for separator instead of comma.

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
  • Thank you very much.But I don't yet understand what to programmatically do. Here is the string form SharedPreferences: `dict_name::149272::go,`. Where should your code be placed? – Niamh Doyle Jan 29 '12 at 15:47
  • @user998032 I have added the code to explain how we can add multiple favorite items. You can use the code as it is, it will work for you. – Muhammad Nabeel Arif Jan 29 '12 at 16:02
  • @@Muhammad Nabeel Arif: Thank you very much, but still no luck, sorry! I want the favourite to be saved after onClick but many errors produced after the above edited code. – Niamh Doyle Jan 29 '12 at 17:04
  • @user998032 I am using same code in my application to maintain history of users, which have successfully loggedin the application. It is working fine on my side. Would please tell me what specific error you are facing. Maybe I can help you. – Muhammad Nabeel Arif Jan 29 '12 at 17:21
  • Many thanks. I don't really know why but when placed as it is after onClick... it shows **Syntax errors**. Are you available to join the chat room now? – Niamh Doyle Jan 29 '12 at 17:37
  • @user998032, I am available to chat. – Muhammad Nabeel Arif Jan 29 '12 at 18:23
  • @user998032, The methods which i have provided should be saved in separate Utility class. Then you can access these methods. I have provided the mechanism about how to use these methods at the top. Hope this will save your problem – Muhammad Nabeel Arif Jan 30 '12 at 05:27
  • Thank you so much for your help. I have applied your updated code but not sure what I did wrong, but it still does not work. I may have to give up. – Niamh Doyle Jan 30 '12 at 11:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7163/discussion-between-niamh-doyle-and-muhammad-nabeel-arif) – Niamh Doyle Jan 30 '12 at 11:44
  • By the way, how can you remove the part of the String from the saved data ? – Jennifer Dec 24 '14 at 03:49
7

SharedPreferences work via simple key/value so when you provide a new value for the same key, the previous value is overwritten. The only way to do what you're trying to do is to use different keys, which sort of hints towards the fact that you probably shouldn't be using SharedPreferences for what you're trying to do.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
5

Honeycomb added the putStringSet method, so you could use that if you don't have to support anything less than Honeycomb:

@Override
public void onClick(View v) {
    SharedPreferences faves = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    {
        Set<String> faveSet = faves.getStringSet("favourite");
        faveSet.add(mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ",");
        SharedPreferences.Editor editor = faves.edit();
        editor.putStringSet("favourite", faveSet);
        editor.commit();
    }
    Log.i(CONTENT_TAG,"Favourite saved!");

    Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
    toast.show();
}

If you need support for pre-Honeycomb devices, you will have to come up with your own scheme.

One possibility is to store the words as comma-separated values in one preference.

Another is to generate a new key for each new word, "favourite1", "favourite2", "favourite3" and have another preference you use to store the number of words.

Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
Brigham
  • 14,395
  • 3
  • 38
  • 48
2

Every time you click the button you save the favorite word with the already present key: favorite and you override it. To save more than one word you have to save the words with different keys. So every time you save a favorite word you could do:

private static int incrementedValue = 0;
...
@Override
public void onClick(View v) {
    SharedPreferences faves = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    SharedPreferences.Editor editor = faves.edit();
    editor.putString("favourite" + incrementedValue, mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ",");
    editor.commit();

    Log.i(CONTENT_TAG,"Favourite saved!");

    Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
    toast.show();
    incrementedValue++;
}
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
user
  • 86,916
  • 18
  • 197
  • 190
  • Thank you very much. I like your approach but when it is applied, it produces errors with **incrementedValue**, saying VariableDeclarationId expected after... – Niamh Doyle Jan 29 '12 at 15:56
  • @user998032 I edited my answer, i forgot to add `int` to the declaration of the incrementedValue variable(don't forget to add the first line, the variable declaration, in your class with the method onClick()). – user Jan 29 '12 at 16:02
  • Thanks a lot, slukian! It works but not as I expected. SharePreferences now saves strings as: `dict_name::157562::heap up, dict_name::31377::bell, dict_name::349333::tell, dict_name::349333::tell, dict_name::11834::am,`, but only the first favourite word ("favourite0") is loaded for view. – Niamh Doyle Jan 29 '12 at 17:09
2

Well that's because the actual preference storage is not a List of strings, just a single one, so whenever you say putString() you overwrite the previous value. A good way to store multiple objects in a single preference string is to use JSON. Simply serialize the value and then write it to them. It also has the benefit of converting directly back into an object of whatever complexity you wish. Look into using Jackson if you decide to go on this route.

dmon
  • 30,048
  • 8
  • 87
  • 96
1

You could use a TreeMap (or other type of list which implements Serializable). Here's how I handled a list of favourites recently. In the TreeMap, Favourite is a class I use. In your case, you could just use TreeMap<Integer, String> instead.

private static TreeMap<Integer, Favourite> favourites;

...
...
...

// load favourites
FileInputStream fis=null;
ObjectInputStream ois = null;
try {
    fis = context.openFileInput(context.getString(R.string.favourites_file));
    try {
    ois = new ObjectInputStream(fis);
    favourites = (TreeMap<Integer, Favourite>) ois.readObject();
    } catch (StreamCorruptedException e) {
    } catch (IOException e) {
    } catch (ClassNotFoundException e) {
    }
} catch (FileNotFoundException e) {
} finally {
    try {
    if (ois!=null){
        ois.close();
    }
    } catch (IOException e) {
    }    
}

if (favourites==null){
    favourites = new TreeMap<Integer, Favourite>();     
}

...
...
...

// save favourites
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
    fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE);
    try {
    oos = new ObjectOutputStream(fos);
    oos.writeObject(favourites);
    } catch (IOException e) {
    }
} catch (FileNotFoundException e) {
} finally {
    if (oos!=null){
    try {
        oos.close();
    } catch (Exception e2) {

    }
    }    
}

You can also avoid the "::" thing you're doing to separate values.

Hope that helps...

Simon
  • 14,407
  • 8
  • 46
  • 61