Home > database >  Get or use Value of sharedpreferences in function class
Get or use Value of sharedpreferences in function class

Time:11-02

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related