我在登录用户 ID 和适配器 Java 访问中有一个获取共享首选项,但红线显示失败getActivity()。
适配器.java代码
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.siparisitemlayout,parent,false);
getSharedPreferences = new GetSharedPreferences(getActivity());
kurye_id=getSharedPreferences.getSession().getString("id",null);
return new ViewHolder(view );
}
获取共享首选项代码
public GetSharedPreferences(Activity activity)
{
this.activity=activity;
}
public SharedPreferences getSession()
{
sharedPreferences = activity.getApplicationContext().getSharedPreferences("",0);
return sharedPreferences;
}
public void setSession(String id,String username)
{
sharedPreferences = activity.getApplicationContext().getSharedPreferences("",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("id",id);
editor.putString("username",username);
editor.commit();
}
料青山看我应如是
相关分类