Android 在适配器中获取上下文
我想知道如何在适配器中获取上下文 我试图在适配器中使用SharedReferences,但是我需要获取上下文,这样它才能工作Android 在适配器中获取上下文,android,sharedpreferences,adapter,android-context,Android,Sharedpreferences,Adapter,Android Context,我想知道如何在适配器中获取上下文 我试图在适配器中使用SharedReferences,但是我需要获取上下文,这样它才能工作 public class ImgAdapter extends BaseAdapter { private Context mContext; private ColorMatrixColorFilter cf; String Question = Question.Pref; public ImgAdapter(Context c)
public class ImgAdapter extends BaseAdapter {
private Context mContext;
private ColorMatrixColorFilter cf;
String Question = Question.Pref;
public ImgAdapter(Context c) {
mContext = c;
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0); //0 means grayscale
cf = new ColorMatrixColorFilter(matrix);
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
//SharedPreferences pref = context.getSharedPreferences(Question, context.MODE_PRIVATE);
---- "I'm trying to use SharedPreferences in there" ----
ImageView imageView;
imageView = (ImageView) convertView;
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
imageView.setImageResource(mThumbIds[position]);
if(Question.answered){
imageView.setColorFilter(cf);
imageView.setAlpha(175);
}
return imageView;
}
public static Integer[] mThumbIds =
{
R.drawable.question1,
R.drawable.question2,
};
}
当我尝试这样做时:
public View getView(int position, View convertView, ViewGroup parent, Context context) {
SharedPreferences pref = context.getSharedPreferences(Question, context.MODE_PRIVATE);
我得到的类型ImgAdapter必须实现继承的抽象方法Adapter.getView(int、View、ViewGroup)
有可能这样做吗?如果有的话,有人能帮我吗
公共视图getView(int位置、视图转换视图、视图组父级、,
上下文){SharedReferences pref=
getSharedReferences(问题,context.MODE_PRIVATE)
因为您有错误的方法签名。您有一个额外的参数Context
,与从超类继承的方法签名不匹配
看,你有这个:
public View getView(int po, View row, ViewGroup parent, Context context) { ... }
它必须是:
public View getView(int position, View row, ViewGroup parent) { ... }
从方法签名中删除上下文参数即可。请注意,您可以访问mContext变量,并且可以执行以下操作:
SharedPreferences pref = mContext.getSharedPreferences(...);
希望它能帮助您解决所面临的问题。您不能用这种方式实现getView()方法 你们都准备好了,上面有上下文
private Context mContext;
在您的首选项中使用此上下文,如下面所示
SharedPreferences pref = context.getSharedPreferences(Question, mContext.MODE_PRIVATE);
mContext@Stav替换
SharedPreferences pref = context.getSharedPreferences(Question, context.MODE_PRIVATE);
到
是否确实要将正确类型的上下文传递给适配器?如果要从活动创建适配器实例,则需要将关键字this传递给适配器的构造函数。如果它来自片段,则需要将getActivit()传递给构造函数 从活动:
ImageAdapter imageAdapter = new ImageAdapter(this);
从片段:
ImageAdapter imageAdapter = new ImageAdapter(getActivity());
您需要检查发送的是哪种上下文。上述内容应该可以正常工作。请确保您使用的是上述内容之一,而不是
getApplicationContext
或任何类似内容。通常,您不需要在getView()中使用任何上下文引用方法,因为它已经为您提供了上下文。请参见父级?这是您的上下文:
parent.getContext()
这就是您所需要的一切。:-)
另一个要点:您的getView()如果不是说错的话,实现似乎效率极低。请使用ViewHolder模式并观看此视频:为什么您不能只使用
mContext
?@blackbelt,因为我尝试了,应用程序崩溃了,为什么会崩溃?请编辑您的问题并添加尝试使用mContext时获得的堆栈跟踪。您确定吗您是否正在将正确类型的上下文
传递给适配器?如果您正在从活动创建适配器实例,则需要将关键字this
传递给适配器的构造函数。如果它来自片段,则需要将getActivit()
传递给构造函数。
parent.getContext()