Home > Mobile >  How to reference "getSharedPreferences" from a static context that is not an Activity
How to reference "getSharedPreferences" from a static context that is not an Activity

Time:09-17

I'm in

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewholder> 

When trying

SharedPreferences mySharedPrefData = getSharedPreferences("mySharedPrefData",MODE_PRIVATE);

I get as an error:

Non-static method 'getSharedPreferences(java.lang.String, int)' cannot be referenced from a static context

I tried as alternatives

Context.getSharedPreferences("mySharedPrefData",MODE_PRIVATE)

and

this.getSharedPreferences("mySharedPrefData",MODE_PRIVATE)

and declaring mySharedPrefData as static

static SharedPreference mySharedPrefData

but all throw errors.

Question: How can I get data from mySharedPrefData into the class MyAdapter?

CodePudding user response:

to use the sharedPreferences you need to have the activity, so there's a lot of options here:

  • if you're using fragments then you need activity to get applicationContext, you can get an activity by calling requireActivity() on your fragment object.

  • then get a SharedPreferences reference to your data through this line of code.

Context context = requireActivity().getApplicationContext();
SharedPreferences sharedPref = context.getSharedPreferences(
        "mySharedPrefData", Context.MODE_PRIVATE);
  • then you can send this sharedPref reference through the adapter constructor when you initialize it. so your adapter constructor code should look like this :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewholder> {
        SharedPreferences myShared ;
        public MyAdapter (SharedPreferences shared){
              myShared = shared ;
        }
}

which from now on you can use the myShared variable to access your shared preferences inside your adapter.

N.B : if you're not using fragments and initializing your adapter inside your main activity directly, then you can skip the requireActivity part and your shared preferences code will look like this instead

Context context = getApplicationContext();
SharedPreferences sharedPref = context.getSharedPreferences(
        "mySharedPrefData", Context.MODE_PRIVATE);

Update

  • since there's some confusion let me clarify that you should use the getApplicationContext() call inside your activity, assuming you initialize your adapter in onCreate method inside activity, then your MainActivity code might look like this :
public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ...
       Context context = getApplicationContext();
       SharedPreferences sharedPref = context.getSharedPreferences(
        "mySharedPrefData", Context.MODE_PRIVATE);
       MyAdapter myAdapter = new myAdapter(sharedPref);
       ...
      }
}

N.B : three dots (...) means that I don't care about your code written here.

Update

  • if you don't have the permission to edit the adapter constructor parameters then what about making a setter to it.

MyAdapter.java

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewholder> {
            SharedPreferences myShared ;
            public MyAdapter (SomeOtherParameter){...}
            public void setMyShared(SharedPreferences shared){
                   myShared = shared ;    
            }
    }
  • then after initializing your adapter, you set the shared Preferences to it

MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ...
       Context context = getApplicationContext();
       SharedPreferences sharedPref = context.getSharedPreferences(
        "mySharedPrefData", Context.MODE_PRIVATE);
       MyAdapter myAdapter = new myAdapter(SomeOtherParameter);
       myAdapter.setMyShared(sharedPref);
       ...
      }
}

N.B : if you used the code above then don't use the myShared object in any case in the adapter constructor to avoid NullPointerException

  • Related