Bikram Nath
Bikram Nath

Reputation: 483

Get the sharedpreference data in an adapter

Googled and tried my whole day about getting the sharedpreference data in my adapter. I have an SharedPreferenceManager class

public class SharedPrefManager {
static SharedPreferences sharedPreferences;
static Context mContext;

static int PRIVATE_MODE = 0;

private static final String PREF_NAME = "sessionPref";

static SharedPreferences.Editor editor;

public SharedPrefManager (Context context) {
    mContext = context;
    sharedPreferences = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = sharedPreferences.edit();
}

public void saveUID(Context context,String uid){
    mContext = context;
    sharedPreferences = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("UID", uid);
    editor.commit();
}
public String getUID(){
    sharedPreferences = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    return sharedPreferences.getString("UID", "");
}
public void clear(){
    editor.clear();
    editor.apply();
}
}

I call the SharedPrefManager in any activity like this

public SharedPrefManager sharedPrefManager;
public String uid

and in onCreate()

sharedPrefManager = new SharedPrefManager(mContext);
uid = sharedPrefManager.getUID();

now, how to get the same data in an adapter since the below code gives error in any adapter

SharedPrefManager sharedPrefManager = new SharedPrefManager(context);
public String uid = sharedPrefManager.getUID();

Here is my Adapter Class

public class MyCommentAdapter extends RecyclerView.Adapter<MyCommentAdapter.MyCommentHolder> {
Context context;
private List<MyComment> commentList;

//Here I get the null pointer error
SharedPrefManager sharedPrefManager = new SharedPrefManager(context);
public String uid = sharedPrefManager.getUID();
private DatabaseReference mDatabaseReference1=FirebaseDatabase.getInstance().getReference().child("User").child(uid).child("Comment");

int the Last line of code I want to access the database with the uid, that is why I need the uid value from shared preference

public MyCommentAdapter(Context context, List<MyComment> commentList) {
    this.context = context;
    this.commentList = commentList;
}

Upvotes: 0

Views: 2017

Answers (4)

Mohsinali
Mohsinali

Reputation: 553

public class SP {
    /**
     * @param mContext
     * @param key
     * @param value
     */
    public static void savePreferences(Context mContext, String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value).apply();
    }

    /**
     * @param context
     * @param keyValue
     * @return
     */
    public static String getPreferences(Context context, String keyValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(keyValue, "");
    }

    /**
     * @param mContext
     */
    public static void removeAllSharedPreferences(Context mContext) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear().apply();
    }
}

Upvotes: 1

Umair
Umair

Reputation: 6426

you need to initialize your sharedPreferancne class in the constructor of the adpater. Something like this:

private CustomSharedPreferences customSharedPreferences;

and then in your constructor:

public MyCommentAdapter(Context context, List<MyComment> commentList) 
{
this.context = context;
this.customSharedPrefernces = new CustomSharedPreferences(context);
this.commentList = commentList;
}

Upvotes: 1

Jyoti JK
Jyoti JK

Reputation: 2171

Your context is null. Try after assigning it

SharedPrefManager sharedPrefManager ;
public String uid ;
private DatabaseReference mDatabaseReference1;
public MyCommentAdapter(Context context, List<MyComment> commentList) {
    this.context = context;
    sharedPrefManager = new SharedPrefManager(context);
    this.commentList = commentList;
    uid = sharedPrefManager.getUID();
    mDatabaseReference1=FirebaseDatabase.getInstance().getReference().child("User").child(uid).child("Comment");

}

Upvotes: 1

Mahdi Nouri
Mahdi Nouri

Reputation: 1389

First of all it's not a right thing to this kind of things inside of your RecyclerView adapter, it slows down you scrolling performance and it's violating SRP.

BUT

you can simply inject you SharedPrefManager into you adapter :

public class MyCommentAdapter extends RecyclerView.Adapter<MyCommentAdapter.MyCommentHolder> {

    private Context context;
    private List<MyComment> commentList;

    private SharedPrefManager sharedPrefManager;
    public String uid;

    public MyCommentAdapter(Context context, List<MyComment> commentList, SharedPrefManager sharedPrefManager) {
        this.context = context;
        this.commentList = commentList;
        this.sharedPrefManager = sharedPrefManager
        this.uid = sharedPrefManager.getUID();
    }
}

and then in your activity when you are creating the adapter you can inject sharedPrefManager in it's constructor.

Upvotes: 1

Related Questions