1

I'm not so sure on how can I store more than one value in shared preferences on an Android app using the same key.

What the app do is show a list and a button to add that item to favorites I want to store that numeric value to preferences and when the user go to the favorite list send all the stored favorites through http post in an array.

EDIT: I'm doing it this way but when I add a new value it overrides the last one, check implode method, I add the new value to the stored list empty or not it has to add the new value and preserve the last ones.

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class FavoriteActivity {

    private static String FAVORITE_LIST = "FAV_LIST";
    private static SharedPreferences sharedPreference;
    private static Editor sharedPrefEditor;

    public static List<NameValuePair> getFavoriteList(Context context) {
        if (sharedPreference == null) {
            sharedPreference = PreferenceManager
                    .getDefaultSharedPreferences(context);
        }
        String storedList = sharedPreference.getString(FAVORITE_LIST, "");
        return explode(storedList);
    }

    public static void saveFavorite(String fav, Context context) {
        if (sharedPreference == null) {
            sharedPreference = PreferenceManager
                    .getDefaultSharedPreferences(context);
        }
        sharedPrefEditor = sharedPreference.edit();
        implode(getFavoriteList(context), fav, 1);
        sharedPrefEditor.putString(FAVORITE_LIST, fav);
        sharedPrefEditor.commit();
    }

    public static List<NameValuePair> explode(String string) {
        StringTokenizer st = new StringTokenizer(string, ",");
        List<NameValuePair> v = new ArrayList<NameValuePair>();
        for (; st.hasMoreTokens();) {
            v.add(new BasicNameValuePair("id[]", st.nextToken()));
        }
        return v;
    }

    public static String implode(List<NameValuePair> list, String value,
            int mode) {
        StringBuffer out = new StringBuffer();
        switch (mode) {
        case 0:
            list.remove(new BasicNameValuePair("id[]", value));
            break;
        case 1:
            list.add(new BasicNameValuePair("id[]", value));
            break;
        }
        boolean first = true;
        for (NameValuePair v : list) {
            if (first)
                first = false;
            else
                out.append(",");
            out.append(v.getValue());
        }
        return out.toString();
    }

}
Shixons
  • 197
  • 3
  • 13

3 Answers3

3

You can't do this. If you use the same key anywhere, it will overwrite the previous value.

Perhaps you could convert your values into an array and store the array, or maybe look into using an SQLite database, in which you can specify the keys in one column, and the corresponding value in another and then run a SELECT statement that selects all rows with the key in it.

Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • I know, that's why I'm asking how can I achieve this, I can use putStringSet() but is only available on API level 11 or higher and my app need to be compatible with API level 8. – Shixons Apr 05 '13 at 17:37
  • Hmmm, is efficient retrieve the array, add the new value and save the array again?. – Shixons Apr 05 '13 at 17:41
  • @Shixons As efficient as any other SharedPrefs operation. – Raghav Sood Apr 05 '13 at 17:44
0

Since api 11 you can put StringSet. Another way is not possible.

Bullman
  • 318
  • 1
  • 4
  • 13
0

OK, this is the way I did it and works perfect, also it doesn't add any duplicates.

private static String FAVORITE_LIST = "FAV_LIST";
private static SharedPreferences sharedPreference;
private static Editor sharedPrefEditor;

public static List<NameValuePair> getFavoriteList(Context context) {
    if (sharedPreference == null) {
        sharedPreference = PreferenceManager
                .getDefaultSharedPreferences(context);
    }
    String storedList = sharedPreference.getString(FAVORITE_LIST, "");
    return explode(storedList);
}

public static void saveFavorite(String fav, Context context) {
    modifyFavorite(fav, context, 1);
}

public static void removeFavorite(String fav, Context context) {
    modifyFavorite(fav, context, 0);
}

private static void modifyFavorite(String fav, Context context, int mode) {
    if (sharedPreference == null) {
        sharedPreference = PreferenceManager
                .getDefaultSharedPreferences(context);
    }
    sharedPrefEditor = sharedPreference.edit();
    String newList = implode(getFavoriteList(context), fav, mode);
    sharedPrefEditor.putString(FAVORITE_LIST, newList);
    sharedPrefEditor.commit();
}

private static List<NameValuePair> explode(String string) {
    StringTokenizer st = new StringTokenizer(string, ",");
    List<NameValuePair> v = new ArrayList<NameValuePair>();
    for (; st.hasMoreTokens();) {
        v.add(new BasicNameValuePair("id[]", st.nextToken()));
    }
    return v;
}

private static String implode(List<NameValuePair> list, String value,
        int mode) {
    StringBuffer out = new StringBuffer();
    switch (mode) {
    case 0:
        list.remove(new BasicNameValuePair("id[]", value));
        break;
    case 1:
        list.add(new BasicNameValuePair("id[]", value));
        break;
    }
    boolean first = true;
    for (NameValuePair v : list) {
        if (out.lastIndexOf(v.getValue()) == -1) {
            if (first) {
                first = false;
            } else {
                out.append(",");
            }
            out.append(v.getValue());
        }
    }
    return out.toString();
}
Shixons
  • 197
  • 3
  • 13