我有一个名为BinderData
的类,它扩展了BaseAdapter
。 我想获取基类的上下文。 我尝试使用getContext()
,但在BaseAdapter
的情况下不起作用。
我该如何获取这个类的上下文?
我有一个名为BinderData
的类,它扩展了BaseAdapter
。 我想获取基类的上下文。 我尝试使用getContext()
,但在BaseAdapter
的情况下不起作用。
我该如何获取这个类的上下文?
另一个解决方案-
如果您有父组件,可以像这样直接访问上下文:
public class SampleAdapter extends BaseAdapter {
LayoutInflater inflater;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater == null){
Context context = parent.getContext();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
...
...
return convertView;
}
}
Context
对象,并将其存储在私有变量中。public class SampleAdapter extends BaseAdapter {
private Context context;
public SampleAdapter(Context context) {
this.context = context;
}
/* ... other methods ... */
}
getApplicationContext()
或this
。 - Aniket ThakurContext context = parent.getContext();