【问题标题】:SharedPreferences: History saved but why not Favourite?SharedPreferences:历史已保存,但为什么不收藏?
【发布时间】:2012-01-30 09:58:03
【问题描述】:

我使用以下代码将单词保存到我的字典应用中的历史记录:

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

History 一切正常。

现在我想修改这段代码来保存最喜欢的单词。几乎每一行代码都是一样的,唯一的区别是改编后的代码(不带onPause())放在:

  btnAddFavourite.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)
    //The code is here...
    }

但它不起作用,并且喜欢的单词没有像历史一样保存。

你们能帮忙吗?非常感谢。

【问题讨论】:

    标签: java android sharedpreferences


    【解决方案1】:

    我正在修改我对this StackOverflow link. 的答案
    您可以通过在单个字符串中添加多个收藏夹来在单个首选项中保存多个收藏夹,每个收藏夹项目用逗号分隔。然后您可以使用convertStringToArray 方法将其转换为字符串数组。这是完整的源代码。
    使用 MyUtility 方法保存多个收藏项。

    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"};
    

    将这些方法保存在单独的实用程序类中

     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
    * 您可以使用任何其他字符串作为分隔符,而不是逗号。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2011-12-17
    • 2018-06-16
    • 2015-02-20
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多