我不太确定如何使用相同的密钥在Android应用上的共享偏好设置中存储多个值。
应用程序的作用是显示一个列表和一个按钮,将该项添加到收藏夹我想将该数值存储到首选项,当用户转到收藏夹列表时,通过数组中的http post发送所有存储的收藏夹。
编辑:我这样做但是当我添加一个新值时会覆盖最后一个,检查implode方法,我将新值添加到存储列表中是否为空添加新值并保留最后一个值。
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();
}
}
答案 0 :(得分:3)
你不能这样做。如果您在任何地方使用相同的密钥,它将覆盖以前的值。
也许您可以将您的值转换为数组store the array,或者可以查看使用SQLite数据库,您可以在其中指定一列中的键,并在另一列中指定相应的值,然后运行SELECT选择包含密钥的所有行的语句。
答案 1 :(得分:0)
从api 11开始,你可以放StringSet。另一种方式是不可能的。
答案 2 :(得分:0)
好的,这就是我做的方式并且工作完美,也没有添加任何副本。
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();
}