Погуглил и целый день пытался получить данные общего доступа в моем адаптере.У меня есть SharedPreferenceManager
класс
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();
}
}
Я называю SharedPrefManager
в любой деятельности, подобной этой
public SharedPrefManager sharedPrefManager;
public String uid
и в onCreate()
sharedPrefManager = new SharedPrefManager(mContext);
uid = sharedPrefManager.getUID();
Теперь, как получить те же данные в адаптере, так как приведенный ниже код дает ошибку в любом адаптере
SharedPrefManager sharedPrefManager = new SharedPrefManager(context);
public String uid = sharedPrefManager.getUID();
Вот мой 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 Последняя строка кодаЯ хочу получить доступ к базе данных с помощью uid, поэтому мне нужно значение uid из общих настроек
public MyCommentAdapter(Context context, List<MyComment> commentList) {
this.context = context;
this.commentList = commentList;
}