Getting “cannot resolve method” error when trying to implement getSharedPreferences in Android Studio

后端 未结 2 1804
挽巷
挽巷 2021-02-02 00:59

I\'m trying to create a class KeyValueDB which stores methods for interacting with SharedPreferences, however I\'m running into a problem just defining the class. All I want th

相关标签:
2条回答
  • 2021-02-02 01:23

    getSharedPreferences() needs a context to be accessed.

    For instance:

    mContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    

    You need to either pass the context into the constructor for KeyValueDB, or a better way would be to access that statically.

    I would do this

    public class KeyValueDB {
    private SharedPreferences sharedPreferences;
    private static String PREF_NAME = "prefs";
    
        public KeyValueDB() {
        // Blank
        }
    
        private static SharedPreferences getPrefs(Context context) {
            return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        }
    
        public static String getUsername(Context context) {
            return getPrefs(context).getString("username_key", "default_username");
        }
    
        public static void setUsername(Context context, String input) {
            SharedPreferences.Editor editor = getPrefs(context).edit();
        editor.putString("username_key", input);
        editor.commit();
        }
    }
    

    Just repeat those get and set methods for any information you need to store.

    To access them from an Activity, you would do this:

    String username = KeyValueDB.getUsername(this);
    

    Where "this" is a reference to the Activity. It's also good practice to setup a context in each Activity in the onCreate() method, like:

    public class myActivity extends Activity{
    
    Context mContext;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
    
        String username = KeyValueDB.getUsername(mContext);
    }
    

    EDIT July 2016

    in response to @RishirajPurohit below, to set a username you do very much the same thing:

    KeyValueDB.setUsername(mContext, "DesiredUsername");
    

    From there everything is done for you in the previous static class, the change is committed to the shared preferences file and persisted and ready to be retrieved by the get method.

    Just a note on the get method, in case anyone wonders:

    public static String getUsername(Context context) {
        return getPrefs(context).getString("username_key", "default_username");
    }
    

    "default_username" is exactly as it sounds. If that get method is called first before a username is set, that is the value that is returned. Less useful in this instance, but when you start using Integers and Boolean values this is very useful for ensuring robustness.

    0 讨论(0)
  • 2021-02-02 01:41

    You should pass a context there...

    example of getting a string value:

    public static String getUserName(Context ctx)
    {
        return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
    }
    

    where PREF_USER_NAME is the key and the second parameter is when it cant find the key it returns ""

    0 讨论(0)
提交回复
热议问题