Android 从共享首选项中放置和获取字符串数组
我需要在共享首选项上保存一些字符串数组,然后再获取它们。 我试过这个:Android 从共享首选项中放置和获取字符串数组,android,arrays,string,sharedpreferences,Android,Arrays,String,Sharedpreferences,我需要在共享首选项上保存一些字符串数组,然后再获取它们。 我试过这个: prefsEditor.putString(PLAYLISTS,PLAYLISTS.toString())其中播放列表是字符串[] 并获得: playlist=myPrefs.getString(PLAYLISTS,“PLAYLISTS”)其中播放列表是一个字符串,但它不工作 我该怎么做?有人能帮我吗 提前感谢。您可以创建自己的数组字符串表示形式,如下所示: StringBuilder sb = new StringBuil
prefsEditor.putString(PLAYLISTS,PLAYLISTS.toString())代码>其中播放列表是字符串[]
并获得:
playlist=myPrefs.getString(PLAYLISTS,“PLAYLISTS”)代码>其中播放列表是一个字符串
,但它不工作
我该怎么做?有人能帮我吗
提前感谢。您可以创建自己的数组字符串表示形式,如下所示:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < playlists.length; i++) {
sb.append(playlists[i]).append(",");
}
prefsEditor.putString(PLAYLISTS, sb.toString());
String[] playlists = playlist.split(",");
这应该可以完成任务。您可以使用JSON将数组序列化为字符串,并将其存储在首选项中。有关类似问题,请参见我的答案和示例代码:
从API级别11,您可以使用putStringSet和getStringSet来存储/检索字符串集:
SharedPreferences pref = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(SOME_KEY, someStringSet);
editor.commit();
SharedPreferences pref = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
Set<String> someStringSet = pref.getStringSet(SOME_KEY);
SharedReferences pref=context.getSharedReferences(标记,context.MODE\u PRIVATE);
SharedReferences.Editor=pref.edit();
putStringSet(SOME_键,someStringSet);
commit();
SharedReferences pref=context.getSharedReferences(标记,context.MODE\u PRIVATE);
Set someStringSet=pref.getStringSet(SOME_键);
HashSet mSet=new HashSet();
mSet.添加(“数据1”);
mSet.添加(“数据2”);
saveStringSet(上下文,mSet);
在哪里
publicstaticvoidsavestringset(上下文上下文,HashSet-mSet){
SharedReferences sp=PreferenceManager.GetDefaultSharedReferences(上下文);
SharedReferences.Editor=sp.edit();
putStringSet(PREF_STRING_SET_KEY,mSet);
editor.apply();
}
及
公共静态集getSavedStringSets(上下文){
SharedReferences sp=PreferenceManager.GetDefaultSharedReferences(上下文);
返回sp.getStringSet(PREF_STRING_SET_KEY,null);
}
私有静态最终字符串PREF\u String\u SET\u KEY=“String\u SET\u KEY”;
如果您需要更多信息,请使用此简单功能将数组列表存储在prefrence中
以及如何从prefrence获取存储的数组列表
public static ArrayList getSerializeArraylist(SharedPreferences sharedPreferences, String key){
ArrayList tempArrayList = new ArrayList();
try {
tempArrayList = (ArrayList) ObjectSerializer.deserialize(sharedPreferences.getString(key, ObjectSerializer.serialize(new ArrayList())));
} catch (IOException e) {
e.printStackTrace();
}
return tempArrayList;
}
那么如何执行getString()?第二个参数是什么?@IgorG.,这完全取决于您,实现您自己的逻辑来检查该字符串中的项数。问题是字符串不能包含用于将它们彼此分隔的字符。使用此方法之前需要进行编码。@Mario Lenci,我使用“;delimiter;;”作为单独的字符串。那么您的解决方案是什么?bluesm?我认为,如果有人从API级别11或更高级别构建应用程序,这比公认的答案更容易。问题是集合没有排序。与String[]不同,我有一个字符串数组,其值为a、0 b、1。现在,在单击设置时,我想更改这些值。如何在这里使用putStringSet。@pa1pal,你可能应该创建一个新问题,并在那里准确地解释你想做什么,以及你到目前为止做了什么。@TomášHubálek指出,Set
没有顺序,因此可能会改变你程序的行为。此链接已断开。ObjectSerializer不是Android中的类。
public static void saveStringSet(Context context, HashSet<String> mSet) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
editor.putStringSet(PREF_STRING_SET_KEY, mSet);
editor.apply();
}
public static Set<String> getSavedStringSets(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getStringSet(PREF_STRING_SET_KEY, null);
}
private static final String PREF_STRING_SET_KEY = "string_set_key";
public static void storeSerializeArraylist(SharedPreferences sharedPreferences, String key, ArrayList tempAppArraylist){
SharedPreferences.Editor editor = sharedPreferences.edit();
try {
editor.putString(key, ObjectSerializer.serialize(tempAppArraylist));
editor.apply();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList getSerializeArraylist(SharedPreferences sharedPreferences, String key){
ArrayList tempArrayList = new ArrayList();
try {
tempArrayList = (ArrayList) ObjectSerializer.deserialize(sharedPreferences.getString(key, ObjectSerializer.serialize(new ArrayList())));
} catch (IOException e) {
e.printStackTrace();
}
return tempArrayList;
}