在适配器

时间:2018-05-29 10:13:43

标签: android sharedpreferences adapter

用Google搜索并尝试了一整天关于在我的适配器中获取共享偏好数据的问题。我有一个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;
}

4 个答案:

答案 0 :(得分:1)

您的 context is null 。分配后尝试

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

}

答案 1 :(得分:1)

您需要在constructor的{​​{1}} 初始化 sharedPreferancne 类。像这样:

adpater

然后在你的构造函数中:

private CustomSharedPreferences customSharedPreferences;

答案 2 :(得分:1)

首先,对于RecyclerView适配器内部的这类内容来说,这不是一件正确的事情,它会减慢你滚动性能并且它违反了 SRP

BUT

您只需注入您的SharedPrefManager到您的适配器:

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

然后在您的活动中创建适配器时,可以在其构造函数中注入sharedPrefManager。

答案 3 :(得分:1)

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