java - Getting "cannot resolve method" error when trying to implement getSharedPreferences in Android Studio -


I'm trying to create a class key Validby that stores ways to interact with shared options , But I'm just going through a problem defining the class. All I want is to store the conservator with the correct file name to share a shared favorite object, but I can get a "method" receipt reference ( Java .lang.String, int) '

I have a string and an int ... I'm not sure what I'm doing wrong. Any help please appreciate!

 < Code> package com.farmsoft.lunchguru.utils; import android.content.Content; import android.content.sharedPreferences; import java.util.ArrayList; import java.util.list; import java.util.Map; import org.json .JSONException; / ** * Created by SxS * 2014/04/28 * / Public Class KeyValueDB {Private SharedPreferences SharedPreferences; Public KeyValueDB (string prefName) {SharedPreferences = getSharedPreferences (prefName, Context.MODE_PRIVATE);}  

getSharedPreferen Ces () A reference needs to be reached

For example:.

  mContext.getSharedPreferences (PREF_NAME, Context.MODE_PRIVATE);   

You need to pass the context to the keyview DDB in the converter, or it must be used in better way.

I will do this

>  public class keyLDB {shared shared references; Private static string PREF_NAME = "prefaces"; Public KeyWDB () {// blank} Private Static SharePreferences getPrefs {reference reference} {reference reference.getSharedPreferences (PREF_NAME, Context.MODE_PRIVATE); } Get public stable string getUsername (reference references) {GetPrefs (reference) .getString ("username_key", "default_username"); } Public Fixed Zero Set UUN Name (Reference Reference, String Input) {SharedPreferences.Editor Editor = getPrefs (Reference) .itit (); Editor.putString ("username_key", input); Editor.commit (); }}  

Repeat the repeat of those stores, which you need to store any information.

To access them from one activity, you will:

string user name = key DB.getuen name (this);

Where "this" is a reference to the activity. Every activity in the OnCreate () method also has a good practice to set up a reference, such as:

  public class increases myActivity activity (reference mContext; crate at public zero (saved bundle) InstanceState) {Super.Nook (Saved Instantstate); MContext = This; String Username = KeyValueDB.getUsername (mContext);}  

Edit July 2016

In response to @RishirajPurohit, you have to set up a username Most do the same thing:

  key Validby.Set Useman (mContext, "DesiredUsername");  

Everything from there to the previous static class The change is committed to the shared priority file and is ready to be established and retrieved by the method obtained.

Just note one thing, in this case anyone wonders:

  public static string getUsername (references Drb) {return getPrefs (reference) .getString ( "username_key", "default_username"); }  

"default_username" is exactly as it sounds. If that method is called for the first time setting the username, then it is the value that comes back. Less useful in this example, but when you start using intrigers and boolean values ​​it is very useful to ensure persistence.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -