我在互联网上找到的许多代码示例中,context
是在适配器的构造函数中获取的。
这个上下文用于获取一个 inflater
,以便在 getView
方法中填充视图。
我的问题是,既然可以这样轻松地获取它,为什么要在构造函数中获取上下文呢?
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;
}
还有没有理由不使用上述方法,因为迄今为止我在使用它时没有遇到任何问题。
getLayoutInflater()
直接使用LayoutInflater
。 - Mohammed Azharuddin Shaikh