使用同一个键在Android应用程序首选项中存储多个值

使用同一个键在Android应用程序首选项中存储多个值,android,http-post,sharedpreferences,Android,Http Post,Sharedpreferences,我不太确定如何使用同一个键在Android应用程序的共享首选项中存储多个值 该应用程序所做的是显示一个列表和一个按钮,将该项目添加到收藏夹中。我想将该数值存储到首选项中,当用户转到收藏夹列表时,通过http post以数组形式发送所有存储的收藏夹 编辑:我是这样做的,但当我添加一个新值时,它会覆盖最后一个值,请选中“内爆方法”,我将新值添加到存储列表中,无论是否为空,它都必须添加新值并保留最后一个值 import java.util.ArrayList; import java.util.Lis

我不太确定如何使用同一个键在Android应用程序的共享首选项中存储多个值

该应用程序所做的是显示一个列表和一个按钮,将该项目添加到收藏夹中。我想将该数值存储到首选项中,当用户转到收藏夹列表时,通过http post以数组形式发送所有存储的收藏夹

编辑:我是这样做的,但当我添加一个新值时,它会覆盖最后一个值,请选中“内爆方法”,我将新值添加到存储列表中,无论是否为空,它都必须添加新值并保留最后一个值

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();
    }

}
import java.util.ArrayList;
导入java.util.List;
导入java.util.StringTokenizer;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.content.SharedReferences.Editor;
导入android.preference.PreferenceManager;
公开课优惠活动{
私有静态字符串FAVORITE_LIST=“FAV_LIST”;
私有静态SharedReference SharedReference;
私有静态编辑器sharedPrefEditor;
公共静态列表getFavoriteList(上下文){
if(SharedReference==null){
SharedReference=首选项管理器
.GetDefaultSharedReferences(上下文);
}
String storedList=SharedReference.getString(收藏夹列表“”);
返回爆炸(存储列表);
}
公共静态void saveFavorite(字符串fav,上下文){
if(SharedReference==null){
SharedReference=首选项管理器
.GetDefaultSharedReferences(上下文);
}
SharedPreferEditor=sharedPreference.edit();
内爆(getFavoriteList(上下文),fav,1);
putString(收藏夹列表,fav);
提交();
}
公共静态列表分解(字符串){
StringTokenizer st=新的StringTokenizer(字符串,“,”);
列表v=新的ArrayList();
对于(;st.hasMoreTokens();){
v、 添加(新的BasicNameValuePair(“id[]”,st.nextToken());
}
返回v;
}
公共静态字符串内爆(列表、字符串值、,
int模式){
StringBuffer out=新的StringBuffer();
开关(模式){
案例0:
删除(新的BasicNameValuePair(“id[]”,value));
打破
案例1:
添加(新的BasicNameValuePair(“id[]”,value));
打破
}
布尔值优先=真;
对于(NameValuePair v:列表){
如果(第一)
第一个=假;
其他的
out.追加(“,”);
out.append(v.getValue());
}
return out.toString();
}
}

您不能这样做。如果在任何位置使用相同的键,它将覆盖以前的值


也许您可以将值转换为数组,或者可以研究使用SQLite数据库,在该数据库中,您可以在一列中指定键,在另一列中指定相应的值,然后运行SELECT语句,选择包含键的所有行。

自api 11以来,您可以将。另一种方法是不可能的。

好的,我就是这样做的,效果很好,而且没有添加任何重复项

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();
}
private静态字符串FAVORITE\u LIST=“FAV\u LIST”;
私有静态SharedReference SharedReference;
私有静态编辑器sharedPrefEditor;
公共静态列表getFavoriteList(上下文){
if(SharedReference==null){
SharedReference=首选项管理器
.GetDefaultSharedReferences(上下文);
}
String storedList=SharedReference.getString(收藏夹列表“”);
返回爆炸(存储列表);
}
公共静态void saveFavorite(字符串fav,上下文){
修改收藏夹(fav,上下文,1);
}
公共静态void removeFavorite(字符串fav,上下文){
modifyFavorite(fav,上下文,0);
}
私有静态void modifyFavorite(字符串fav、上下文上下文、int模式){
if(SharedReference==null){
SharedReference=首选项管理器
.GetDefaultSharedReferences(上下文);
}
SharedPreferEditor=sharedPreference.edit();
字符串newList=内爆(getFavoriteList(上下文)、fav、模式);
putString(收藏夹列表、新列表);
提交();
}
私有静态列表分解(字符串){
StringTokenizer st=新的StringTokenizer(字符串,“,”);
列表v=新的ArrayList();
对于(;st.hasMoreTokens();){
v、 添加(新的BasicNameValuePair(“id[]”,st.nextToken());
}
返回v;
}
私有静态字符串内爆(列表、字符串值、,
int模式){
StringBuffer out=新的StringBuffer();
开关(模式){
案例0:
删除(新的BasicNameValuePair(“id[]”,value));
打破
案例1:
添加(新的BasicNameValuePair(“id[]”,value));
打破
}
布尔值优先=真;
对于(NameValuePair v:列表){
if(out.lastIndexOf(v.getValue())=-1){
如果(第一){
第一个=假;
}否则{
out.追加(“,”);
}
out.append(v.getValue());
}
}
return out.toString();
}

我知道,这就是为什么我要问如何实现这一点,我可以使用putStringSet(),但只能在API 11级或更高级别上使用,我的应用程序需要与API 8级兼容。嗯,检索阵列是否高效,添加新值并再次保存数组?@Shixons与任何其他SharedPrefs操作一样高效。为什么我永远不能成为第一名-_-但它需要与API 8兼容:(.putString()如何:hello,my,name,is…,然后他们检索它并用逗号分隔?我这样认为,但是…我如何删除值?。仅用“”替换要删除的值