如何使用共享preferences保存多个值? [英] How to use SharedPreferences to save more than one values?

查看:186
本文介绍了如何使用共享preferences保存多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个字典应用程序。在我的应用程序,我认为用户想保存最喜欢的话。我决定使用共享preferences 以保存这些值(我知道的SQLite和文件是更好,但我坚持要共享preferences,所以要与它)

I am developing a dictionary app. In my app, I assume that user wants to save favourite words. I have decided to use SharedPreferences to save these values (I am aware that SQLite and files are better but I am stuck to "SharedPreferences", so keep on with it).

下面下面是我的code:

Here below is my code:

@Override
public void onClick(View v) {                                       
    SharedPreferences faves = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    { 
        SharedPreferences.Editor editor = faves.edit();
        editor.putString("favourite", mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ",");
        editor.commit();    
    }
    Log.i(CONTENT_TAG,"Favourite saved!");

    Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
    toast.show();   
}

的问题是,它不保留多个最喜爱的文字。我的意思是只有一个最喜欢的词被保存,当一个新的添加,在previous被擦除。

The problem is that it does not retain more than one favourite word. I mean only one favourite word is saved and when a new one is added, the previous is erased.

那么,怎样才能上述code进行编辑,这样这个问题就解决了​​?

你们可以有帮助吗?非常感谢你。

Can you guys there help? Thank you very much.

推荐答案

您可以通过在一个单一的字符串添加众多的收藏在一个单一的preference保存多个收藏夹,每一个收藏项目相隔逗号。然后你可以使用 convertStringToArray 方法将其转换成字符串数组。下面是完整的源代码code。
使用MyUtility方法保存多个最喜欢的项目。

You can save multiple favorites in a single preference by adding numerous favorites in a single string, each favorite item separated by comma. Then you can use convertStringToArray method to convert it into String Array. Here is the full source code.
Use MyUtility Methods to save multiple favorite items.

            MyUtility.addFavoriteItem(this, "Sports");
            MyUtility.addFavoriteItem(this, "Entertainment");

获得已保存所有收藏夹String数组

get String array of all favorites saved

String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"};

保存在单独的实用工具类这些方法

Save these methods in separate Utility class

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

如果你有添加额外的收藏夹。然后得到喜爱的字符串,从共享preference 并添加逗号+最喜欢的项目,并将其保存回共享preference
*您可以使用其他任何字符串分隔,而不是逗号。

If you have to add extra favorites. Then get favorite string from SharedPreference and append comma+favorite item and save it back into SharedPreference.
* You can use any other string for separator instead of comma.

这篇关于如何使用共享preferences保存多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆