1

I am using SharedPreferences.Editor in an Activity. How to use getSharedPreferences value in this function ? I am new in java android.

public class Dataall {
    public static String calc(String nfirst, String nsecond) {
        int abn1;
        int abn2;
        int abn3;

    abn1= Integer.parseInt(nfirst);
    abn2 = Integer.parseInt(nsecond);
    // SharedPreferences show error but its work in another Activity
    SharedPreferences prefrs = getSharedPreferences("MY_PRE_NAME", 
    MODE_PRIVATE);
    String langu = prefrs.getString("callss", "");
    if(langu.equals("first")){
        abn1 = 15;
    }else{
        abn1 = 20;
    }
    abn3 = abn1+abn2;
    String res = String.valueOf(res);
    return res;
    
    }

Edited- I am not able to use SharedPreferences in above Static function.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
hitakb
  • 11
  • 5
  • The looks right to me. What are you trying to accomplish. Like, what is the expected result and what is the issue? Your logic is all over the place. Please clean it up so that we may have a better understanding of what you are trying to do. Because right now. The method takes in String nfirst, then you parse it as an int for abn1. Only to overwrite the value of abn1 in your if/else statement to 15 or 20. – JonR85 Oct 30 '21 at 20:03
  • @JonR85 `SharedPreferences` not working in above class or i am new in java so having probem – hitakb Nov 01 '21 at 20:38
  • What's the error message? – JonR85 Nov 01 '21 at 20:46
  • Thanks @JonR85 ! error is **Cannot resolve method 'getSharedPreferences' in Dataall** and **Cannot resolve symbol 'MODE_PRIVATE'** – hitakb Nov 01 '21 at 21:22

2 Answers2

0

There is a good deal here that I want to unpack. Part of this will be explaining what I think you want. You have a class that is doing some calculation that requires you get a preference stored in SharedPreferences. The problem being that most what you call function classes do not have access to Android's context (granted you only need application context for this). So you have many options but here are two.

1.) The quick and dirty - Either pass in context from as far back as you can (Activity or Fragment) into this method and use that to get the shared preferences like such.

public class Dataall {
public static String calc(String nfirst, String nsecond, Context context) {
    int abn1;
    int abn2;
    int abn3;

    abn1= Integer.parseInt(nfirst);
    abn2 = Integer.parseInt(nsecond);
    SharedPreferences prefrs = PreferenceManager.getDefaultSharedPreferences(context);
    String langu = prefrs.getString("callss", "");
    if(langu.equals("first")){
        abn1 = 15;
   }else { 
    abn1 = 20;
    }
    abn3 = abn1 +a bn2;
    String res = String.valueOf(res);
    return res;
}
  1. Extract the shared preferences away through DI or Service Location or (worst option just listing it as a possibility because it technically works) making it a singleton with a wrapped interface. Granted you will need to make a custom application class to get the context

    //Option 1 pass in to make it mockable

    public class Dataall { public static String calc(String nfirst, String nsecond, ISettingsProvider settings) { int abn1; int abn2; int abn3;

     abn1= Integer.parseInt(nfirst);
     abn2 = Integer.parseInt(nsecond);
    
     String langu = settings.getString("callss");
     if("first".equals(langu)){
         abn1 = 15;
     }else {
        abn1 = 20;
     }
     abn3 = abn1 +a bn2;
     String res = String.valueOf(res);
     return res;
     }
    

    }

//Option 2 ASSUMING getFirstInstance has been called somewhere that has context

public class Dataall {
    public static String calc(String nfirst, String nsecond) {
        int abn1;
        int abn2;
        int abn3;
    
        abn1= Integer.parseInt(nfirst);
        abn2 = Integer.parseInt(nsecond);
        
        ISettingsProvider settings = SettingsProvider.getInstance();
        String langu = settings.getString("callss");
        if("first".equals(langu)){
            abn1 = 15;
        }else{
           abn1 = 20;
        }
        abn3 = abn1 +a bn2;
        String res = String.valueOf(res);
        return res;
    }
}

public final class SettingsProvider implements ISettingsProvider {
public static bool hasBeenInitialized = false
private static ISettingsProvider instance 
private SharedPreferences prefs = null
public static ISettingsProvider getFirstInstance(Context: context){
    prefs = PreferenceManager.getDefaultSharedPreferences(context);
    instance = SettingsProvider();
    hasBeenInitialized = true;
}

public static ISettingsProvider getInstance(){
    if(instance == null || hasBeenInitialized){
        throw new IllegalStateException("Singleton not initialized");
    }
    return instance;
}

private SettingsProvider(){}

//Overrides
@Override
public String getString(String key, String defaultValue){
    return if (defaultValue != null) prefs.getString(key, defaultValue) else prefs.getString(key, "");
}


}

public interface ISettingsProvider{
//put all your get|put methods here

public String getString(String key);
}
Kerry
  • 283
  • 2
  • 16
0

Change this line: From:

SharedPreferences prefrs = getSharedPreferences("MY_PRE_NAME", 
MODE_PRIVATE);

To:

SharedPreferences prefrs = getSharedPreferences("MY_PRE_NAME", 
Context.MODE_PRIVATE);

Answer taken from here: Getting "cannot resolve method" error when trying to implement getSharedPreferences in Android Studio

JonR85
  • 700
  • 4
  • 12