我使用以下代码在我的词典应用程序中将单词保存到历史记录:
@Override
public void onPause()
{
super.onPause();
saveHistoryToPreferences();
}
public void saveHistoryToPreferences()
{
if (prefs.getBoolean("saveHistory", true) && mWordHistory != null && mWordHistory.size() >= 1)
{
StringBuilder sbHistory = new StringBuilder();
for (String item : mWordHistory)
{
sbHistory.append(item);
sbHistory.append(",");
}
String strHistory = sbHistory.substring(0, sbHistory.length()-1);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("history", strHistory);
editor.commit();
//Log.i(CONTENT_TAG,"history = " + strHistory);
Log.i(CONTENT_TAG,"History saved!");
}
}
public void loadHistoryFromPreferences()
{
if (prefs.getBoolean("saveHistory", true))
{
String strHistory = prefs.getString("history", "");
Log.i(CONTENT_TAG, "History loaded");
if (strHistory != null && !strHistory.equals(""))
{
mWordHistory = new ArrayList<String>(Arrays.asList(strHistory.split(",")));
}
else
{
if (mWordHistory == null)
{
mWordHistory = new ArrayList<String>();
}
else
{
mWordHistory.clear();
}
}
}
else
{
if (mWordHistory == null)
{
mWordHistory = new ArrayList<String>();
}
else
{
mWordHistory.clear();
}
}
}
历史上一切都很好。
现在我想调整此代码以保存喜欢的单词。几乎每个代码行都是相同的,唯一的区别是改编的代码(没有onPause()
)放在:
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
//The code is here...
}
但它不起作用,最喜欢的单词不会像历史一样保存。
你们有帮助吗?非常感谢你。答案 0 :(得分:1)
我正在修改this StackOverflow link.的答案
您可以在一个首选项中保存多个收藏夹,方法是在一个字符串中添加多个收藏夹,每个收藏项以逗号分隔。然后,您可以使用convertStringToArray
方法将其转换为String Array。这是完整的源代码
使用MyUtility Methods保存多个喜欢的项目。
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
MyUtility.addFavoriteItem(MyActivity.this, "Sports");
MyUtility.addFavoriteItem(MyActivity.this, "Entertainment");
}
获取所有收藏夹保存的字符串数组
String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"};
将这些方法保存在单独的Utility类中
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;
}
}
如果您必须添加额外收藏。然后从SharedPreference
获取最喜欢的字符串并附加逗号+收藏项并将其保存回SharedPreference
。
*您可以使用任何其他字符串作为分隔符而不是逗号。